0

I have a file and each line has some info and a date,so what I want is to print the lines with dates after a given date. I use this awk command

sort -t$'|' -k5 $2 | awk -F '|' -v DatedAfter=$4 '!/^#/ && $5>=DatedAfter {print $0}'

Where $4 is a date in DD/MM/YYYY format given and $2 is the file I use in the format bellow.

1099511628908|Chen|Wei|female|02/08/1989|2010-05-24T20:52:26.582+0000|27.98.244.108|Firefox
1099511633435|Smith|Jack|male|19/04/1978|2010-05-26T03:45:11.772+0000|50.72.193.218|Internet Explorer

So when i execute my code i get these results.

./tool.sh --born-since 17/11/1983 -f events.dat
1099511629352|Nunez|Jorge|female|17/11/1986|2011-04-04T05:54:52.693+0000|201.221.59.59|Opera|Facebook
1099511638548|Phan|Don|female|17/5/1981|2011-04-19T00:14:15.041+0000|112.72.79.36|Opera|Flickr
1099511638387|Znaimer|Moses|male|17/8/1980|2011-05-12T01:03:01.291+0000|94.199.19.90|Internet Explorer|Youtube
6597069776449|Svensson|Ernst|male|30/11/1981|2012-04-22T05:16:03.557+0000|31.31.166.207|Opera|Youtube
2199023258994|Ngoche|Alex Obanda|female|30/9/1987|2011-07-22T16:36:27.420+0000|41.81.41.21|Opera|Google+
7696581405294|Dobrunov|Aleksandr|male|31/12/1989|2012-05-15T05:46:31.439+0000|31.25.243.122|Internet Explorer|LinkedIn
2199023266450|Charoenpura|Somchai|male|4/12/1987|2011-08-30T20:34:41.524+0000|110.76.154.132|Mozilla|Youtube
3298534890514|Chen|Hsin|male|4/4/1988|2011-11-03T16:32:44.238+0000|115.42.116.30|Safari|Google+
2199023261081|Ben Dhifallah|Karim|male|6/4/1980|2011-06-11T02:24:17.194+0000|193.95.74.75|Chrome|Twitter
8796093024550|Yang|Lei|male|7/1/1990|2012-07-15T17:14:42.186+0000|1.4.92.176|Mozilla|Facebook
  • I assume (please confirm) that the first line of code in your question is part of a script which expects a filename as the 2nd parameter and a date (in the format of DD-MM-YYYY?) as the 4th parameter? – Jeff Schaller Nov 17 '18 at 12:51
  • Not exactly, the command i use is like this ./exec.sh -f <file> --born-since <DD/MM/YYYY>. – Μάριος Τσοκανάς Nov 17 '18 at 12:55
  • Please edit that incoming date format into the question, so answerers knows how to compare it. Thank you! – Jeff Schaller Nov 17 '18 at 13:20
  • May I doubt that it works with the first date format either? – RudiC Nov 17 '18 at 22:33
3

It will be easier if the dates are in YYYYMMDD (then they will be in lexicographic and numeric order). You can use gensub for that in awk. For example:

awk -F"|" -v d="$d" -v dp='(..)/(..)/(....)' 'BEGIN {gensub(dp, "\3\2\1", d)} {dt=$5; gensub(dp, "\3\2\1", dt);} dt >= d' foo

The d variable holds the date for comparison, and dp holds the pattern matching a DD/MM/YYYY date. Then with gensub, we move around those (\3, \2, \1 being the matched groups ((....), the second (..), the first (..), respectively). Same with the fifth field of each line, which we copy to avoid modifying the input.

I expanded your example input with some more dates:

1099511628908|Chen|Wei|female|02/08/1989|2010-05-24T20:52:26.582+0000|27.98.244.108|Firefox
1099511633435|Smith|Jack|male|19/04/1978|2010-05-26T03:45:11.772+0000|50.72.193.218|Internet Explorer
1099511633435|Smith|Jack|male|19/04/1979|2010-05-26T03:45:11.772+0000|50.72.193.218|Internet Explorer
1099511633435|Smith|Jack|male|19/04/1977|2010-05-26T03:45:11.772+0000|50.72.193.218|Internet Explorer
1099511633435|Smith|Jack|male|19/04/1980|2010-05-26T03:45:11.772+0000|50.72.193.218|Internet Explorer
1099511633435|Smith|Jack|male|19/03/1978|2010-05-26T03:45:11.772+0000|50.72.193.218|Internet Explorer

And got this result using 19/04/1978 for comparison:

1099511633435|Smith|Jack|male|19/04/1978|2010-05-26T03:45:11.772+0000|50.72.193.218|Internet Explorer
1099511633435|Smith|Jack|male|19/04/1979|2010-05-26T03:45:11.772+0000|50.72.193.218|Internet Explorer
1099511633435|Smith|Jack|male|19/04/1980|2010-05-26T03:45:11.772+0000|50.72.193.218|Internet Explorer
1

How about

DT="01/08/1989"
awk -F\| -vDT=${DT:6}${DT:3:2}${DT:0:2} 'substr($5, 7) substr($5, 4, 2) substr($5, 1, 2) > DT' file
1099511628908|Chen|Wei|female|02/08/1989|2010-05-24T20:52:26.582+0000|27.98.244.108|Firefox

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.