Need to filter data based on sysdate

I have a file from which I need to filter out certain lines when field 17 is less than sysdate. The file has date in YYYYMMDD HH:MI:SS format.
Sample file is as below:

PRUM,67016800          ,CC ,C1,67016800          ,00,Y,Y,2 ,US,BX,BOX                                     ,00000001,EA,00000001,20141120 00:00:00,                 ,N,Y,Y,US CUSTOM ,IN,000000047.0000,000000000.7500,000000005.7500,CI,000000202.6875,LB,000000000.6000,000000000.6000,20705032020722,                   ,A,                 ,20141120 00:00:00,   ,GS1 128   ,1.0000,                              ,09975284

With what we showed you in your last thread; what have to done to try to solve this problem yourself?

I am trying to do something like this:

TODAYS_DATE=`date "+%Y%m%d %H:%M:%S"`
echo $TODAYS_DATE
awk 'BEGIN{FS=",";OFS=","} ($17 >  $TODAYS_DATE) {print $0}' file.txt

Hello mady135,

Could you please try following and let us know if this helps. Let's say folowing is the input file.

Input file:

cat file347
PRUM,67016800          ,CC ,C1,67016800          ,  ,Y,Y,2 ,CK,BX,FOX                                     ,00000001,EA,00000001,20141120 00:00:00,               ,N,Y,Y,CK ABCDEF ,IN,000000047.0000,000000000.7500,000000005.7500,CI,000000202.6875,LB,000000000.6000,000000000.6000,20705032020722,                   ,A,                 ,20141120 00:00:00,   ,GS1 128   ,1.0000,                              ,09975284
PRUM,504608X           ,CC ,C1,504608X           , N ,Y,Y,2 ,CK,BX,FOX                                     ,00000005,EA,00000005,20141120 00:00:00,                 ,N,Y,Y,CK ABCDEF ,IN,000000010.2500,000000003.5000,000000004.2500,CI,000000152.4688,LB,000000000.5500,000000000.5500,20705032010150,                   ,A,                 ,20141120 00:00:00,   ,GS1 128   ,1.0000,                              ,09975172
PRUM,504608X           ,CC ,C1,504608X           , N ,Y,Y,2 ,CK,BX,FOX                                     ,00000005,EA,00000005,20151120 00:00:00,                 ,N,Y,Y,CK ABCDEF ,IN,000000010.2500,000000003.5000,000000004.2500,CI,000000152.4688,LB,000000000.5500,000000000.5500,20705032010150,                   ,A,                 ,20141120 00:00:00,   ,GS1 128   ,1.0000,                              ,09975172
PRUM,67016800          ,CC ,C1,67016800          , U ,Y,Y,2 ,CK,BX,FOX                                     ,00000001,EA,00000001,20141120 00:00:00,               ,N,Y,Y,CK ABCDEF ,IN,000000047.0000,000000000.7500,000000005.7500,CI,000000202.6875,LB,000000000.6000,000000000.6000,20705032020722,                   ,A,                 ,20141120 00:00:00,   ,GS1 128   ,1.0000,                              ,09975284

Code is as follows:

awk -F, -vDATE=`date +%Y%m%d` '{A=$16;gsub(/[[:space:]].*/,X,$16);if($16 <= DATE && $16 != ""){next} else {$16=A;print $0}}' OFS=, file347

Output will be as follows.

PRUM,504608X           ,CC ,C1,504608X           , N ,Y,Y,2 ,CK,BX,FOX                                     ,00000005,EA,00000005,20151120 00:00:00,                 ,N,Y,Y,CK ABCDEF ,IN,000000010.2500,000000003.5000,000000004.2500,CI,000000152.4688,LB,000000000.5500,000000000.5500,20705032010150,                   ,A,                 ,20141120 00:00:00,   ,GS1 128   ,1.0000,                              ,09975172

Thanks,
R. Singh

Try

$ awk  -F, '{c=$16; gsub(/[: ]/,"",c)} c > sys' sys="$(date +'%Y%m%d%H%M%S')" infile
$ awk -F, 'BEGIN{sys = strftime("%Y%m%d%H%M%S")}{c=$16; gsub(/[: ]/,"",c)} c > sys'  infile 

Since the date data in the file is in the correct order (most significant data comes first in the string) and the fields (year, month, day, hour, minute, and second) are all fixed length fields, the gsub() calls can be avoided if the system date is formatted the same way the dates are formatted in the file:

awk -F, '$16 > sys' sys="$(date +'%Y%m%d %H:%M:%S')" infile

or, if the GNU time extensions are in your version of awk ,

awk -F, 'BEGIN{sys = strftime("%Y%m%d %H:%M:%S")} $16 > sys' infile