I have a text file with several fields in the following format.
Name:Phone:Address:Date of birth:Salary
The date of birth is in the format mm/dd/yy.
I am having no idea on how to calculate age of the specific person by subtracting their birth year from current year. I need to extract the age then compare it with certain age group lets say 50. I tried some stuff but it gave me weird numbers like
awk -F: ‘{print $4-d}’ “d=$(date)” filename
I have a text file with several fields in the following format. Name:Phone:Address:Date of
Share
You may like to try:
EDIT 1:
To simply print a list of the people who are less than 60 years old, try:
Explanation:
I’m assuming a basic understanding of
awk. The-voption allowsawkto read in a variable from the shell. In this case,date +"Y"simply returns the current year.awkhas asplitfunction that allows you to split a field. In this case, the fourth field containing our date has/separating the months/days/years.splitsplits things into arrays. In this case, I’ve named the arraydob(date of birth). The third field (1 indexed) contains the year of birth. Then some quick maths in a conditional to check that the age of the person is 60+. If he is print out his name in the first field.Edit 2:
After thinking about your question a little more, it’s obvious that the above approach does not actually calculate things perfectly. It was a rough quick job (I’m sorry, well …). So here’s an updated version that will be much, much more accurate. Run like:
Contents of
script.awk:Edit 3:
Or without external commands and getline: