Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8727063
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:22:11+00:00 2026-06-13T08:22:11+00:00

I have a text file with several fields in the following format. Name:Phone:Address:Date of

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T08:22:12+00:00Added an answer on June 13, 2026 at 8:22 am

    You may like to try:

    awk -F: -v year=$(date +"%Y") '{ split($4, dob, "/"); print $1, "is", year-dob[3], "years old" }' file.txt
    

    EDIT 1:

    To simply print a list of the people who are less than 60 years old, try:

    awk -F: -v year=$(date +"%Y") '{ split($4, dob, "/"); if (year-dob[3] <= 60) print $1 }' file.txt
    

    Explanation:

    I’m assuming a basic understanding of awk. The -v option allows awk to read in a variable from the shell. In this case, date +"Y" simply returns the current year. awk has a split function that allows you to split a field. In this case, the fourth field containing our date has / separating the months/days/years. split splits things into arrays. In this case, I’ve named the array dob (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:

    awk -f script.awk file.txt
    

    Contents of script.awk:

    BEGIN {
        FS=":"
        "date +\"%s\"" | getline cdate
    }
    
    {
        rdate = gensub(/([0-9]+)\/([0-9]+)\/([0-9]+)/, "\\3-\\1-\\2", "g", $4)
        cmd = "date -d " rdate " +\"%s\""
    
        while (( cmd | getline result ) > 0 ) {
    
            if ((cdate - result) / 31556926 <= 60) {
                print $1
            }
        }
    }
    

    Edit 3:

    Or without external commands and getline:

    BEGIN {
        FS=":"
        cdate = systime()
    }
    
    {
        rdate = gensub(/([0-9]+)\/([0-9]+)\/([0-9]+)/, "\\3 \\1 \\2 0 0 0", "g", $4)
        result = mktime(rdate)
    
        if ((cdate - result) / 31556926 <= 60) {
           print $1
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a text file with several lines in the following format: gatename #outputs
I have several areas where I use AJAX to submit text fields. var name
I have a text file with several different sections. Each section has a header
I have text file, like FILED AS OF DATE: 20090209 DATE AS OF CHANGE:
I have text file with several thousands lines. I want to parse this file
I have a CSV-file delimited by colons, but it contains text-fields wrapped in quotes,
If I have a text file, which say is comma delimited and has several
I have a webpage that contains a form with several text fields and an
I have several inputs, they are created dynamically <input style=width:100% class=fileDescription type=text name=' +
I have text file with some stuff that i would like to put into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.