0

Input file

ID,Name,join_date
21433432,rds,2014_02_01
2131321,ABCDS,2014-10-20
2432745314,ASRER, 2015-01-20
2132432423,safdsad, 2015-12-30

Need two files created one for records with the date column less than a month and less than 6 months

output file 1 with less than month

ID,name,join_date
2432745314,ASRER, 2015-01-20
2132432423,safdsad, 2015-12-30

output file 2 less than 6 months

2131321,ABCDS,2014-10-20

using the awk command but not working.

1
  • 1
    Is 2014_02_01 a typo? "output file 2 less than 6 months" is supposed to mean "at least one month but less than 6 months"? Jan 24, 2015 at 0:31

2 Answers 2

1

This will get you started. Requires GNU awk for the time functions:

gawk -F, '
    function totime(ymd) {gsub(/[-_]/," ",ymd); return mktime(ymd " 0 0 0")}
    BEGIN {now = systime(); m1 = now - 86400 * 30; m6 = now - 86400 * 180}
    FNR == 1 {next}
    {t = totime($3)}
    t > m1 {print "m1", $0; next}
    t > m6 {print "m6", $0}
' file
m6 2131321,ABCDS,2014-10-20
m1 2432745314,ASRER, 2015-01-20
m1 2132432423,safdsad, 2015-12-30

It's not exactly 1 and 6 months, it's 30 and 180 days, plus or minus an hour for daylight saving transitions

0

Just play with date command inside awk and redirect output from print to desired files according to certain conditions:

awk -F, 'BEGIN{ "date -d\"month ago\" +%s" | getline T1; close("date"); 
  "date -d\"6 months ago\" +%s" | getline T6; close("date")}
  { "date -d" $3 " +%s" | getline t; close("date"); 
  if(t>T1){print $0>"file2";next} if(t>T6)print $0>"file3"}' file

The crucial part is a transform format of date to number of seconds since 1970 with date -d ... "+%s" command. The rest should be self explanatory.

You must log in to answer this question.

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