Reputation: 544
I am trying to filter the data in a text file. There are 2 fields in the text file. The first one is text while 2nd one has 3 parts seperated by _. The first part in the second file is date in yyyyMMdd format and the next 2 are string:
xyz yyyyMMdd_abc_lmn
Now I want to filter the lines in the file based on the date in the second field. I have come up with the following awk command but it doesn't seems to work as it is outputting the entire file definitely I am missing something.
Awk command:
awk -F'\t' -v ldate='20140101' '{cdate=substr($2, 1, 8); if( cdate <= ldate) {print $1'\t\t'$2}}' label
Upvotes: 1
Views: 1337
Reputation: 204468
Try this:
awk -v ldate='20140101' 'substr($NF,1,8) <= ldate' label
Upvotes: 1
Reputation: 77175
Try:
awk -v ldate='20140101' '{split($2,fld,/_/); if(fld[1]<=ldate) print $1,$2}' file
Note:
split
function which basically splits the field based on regex
provided as the third element and stores the fields in the array defined as second element. -F'\t
unless your input file is tab-delimited
. The default value of FS
is space, so defining it to tab
might throw it off in interpreting $2
. To output with two tabs you can set the OFS
variable like:
awk -F'\t' -v OFS='\t\t' -v ldate='20140101' '{split($2,fld,/_/); if(fld[1]<=ldate) print $1,$2}' file
Upvotes: 3