I need to compare two dates for each line of a .txt file. If value in position 11 is equal to "02407IC" and "21100220" minus "19860220" equal to 124 years, I want to bypass this record, otherwise I want this record written to the output.

So far I tried sed command as follows but it only solves part of this bypassing/deleting records from the file were it finds value of 02407IC, but I cannot figure out how to compare two dates and include that comparison into sed command.

sed -i '/02407IC/d' MARK.TXT

This is a sample data in the file, below is not real file but values that I am looking for in the input file. In the real input file value like '02407IC' is in position 398, lower date like '19860220' in position 265 (always present/populated) and higher date like '21100220' is in position 690, sometimes higher date can be blank/spaces:

19860220 02407IC 21100220
19660121 02501IC 20200218
19670301 02501IC 20200218
19880420 03105IC         

I am trying to accomplish this with sed or awk command.

  • See stackoverflow.com/help/formatting for how to format your data and include the expected output for your posted sample input. Also include anything you've tried yourself so far.
    – Ed Morton
    Feb 19, 2020 at 14:55
  • 1
    Welcome to the site. What do you mean with "position 11"? The only field with value "02407" in your sample input is line 1 column 2. Also, please specify how the input fields are separated (space, tab, ...)
    – AdminBee
    Feb 19, 2020 at 15:01
  • Do you want to skip the empty lines, too, or are they supposed to appear in the output?
    – AdminBee
    Feb 19, 2020 at 15:18
  • File is space delimited. Value 02407IC starts in position 11.
    – mamtsism
    Feb 19, 2020 at 15:18
  • 1
    Does the date difference have to be 124 years to the day in order to merit exclusion? If not, how to handle cases where it is 124 years +/- 1 ... 364 (or 365, in case of leap years) days?
    – AdminBee
    Feb 19, 2020 at 15:27

1 Answer 1


It sounds like this might be what you're trying to do:

awk '!( ($2 == "02407IC") && (($3 - $1) ~ /^124....$/) )' file

I'm not sure what the "positions" you're referring to are but if they're character positions in each line and if field 1 starts at character position 265, field 2 at 398, and field 3 at 690 per your updated question, then you could do:

awk '!( (substr($0,398,7) == "02407IC") && ((substr($0,690,4) - substr($0,265,4)) == 124) )' file
  • I am only showing portion of the file, this is space delimited file, can your please show how to use fields position instead of columns $1 or $2 etc?
    – mamtsism
    Feb 19, 2020 at 15:26
  • Thank you guys, that worked I just changed to (substr($0,398,7) == "02407IC") so it looked at all 7 bytes of the field.
    – mamtsism
    Feb 19, 2020 at 15:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .