Reputation: 23
I am a starter. I have a below file in which there are 4 columns; separated with pipe sign.
test.unl
XCS|10|20|20200505|
AWX|20|10|20200606|
WSX|20|10|20200517|
RFV|20|10|20200520|
TGB|10|20|20200609|
I need to write a command, if column 2 is 10 and column 3 is 20 then subtract the current date from last column, if the difference between dates is greater than 30, print the whole line in a file.
I am running the below command and it is giving me error.
more testfile.unl | awk -F '|' '{if(($2==10 && $3==20) && (((date -d "now" +%s)-(date -d "print$4" +%s)/86400))>>30) print$0}' >> File2.unl
Following are the errors i am receiving upon running the command:
awk: {if(($2==10 && $3==20) && ((date -d "print$4" +%s)-(date -d "now" +%s)/86400)>>30) print$0}
awk: ^ syntax error
awk: {if(($2==10 && $3==20) && ((date -d "print$4" +%s)-(date -d "now" +%s)/86400)>>30) print$0}
awk: ^ syntax error
awk: {if(($2==10 && $3==20) && ((date -d "print$4" +%s)-(date -d "now" +%s)/86400)>>30) print$0}
awk: ^ syntax error
awk: {if(($2==10 && $3==20) && ((date -d "print$4" +%s)-(date -d "now" +%s)/86400)>>30) print$0}
awk: ^ syntax error
awk: {if(($2==10 && $3==20) && ((date -d "print$4" +%s)-(date -d "now" +%s)/86400)>>30) print$0}
awk: ^ unterminated regexp
Looking forward to your help, thanks!
Upvotes: 1
Views: 123
Reputation: 133518
EDIT: Using systime
function of awk
rather than using date
shell command try following please.
awk -v thres="30" '
BEGIN{
FS=OFS="|"
current_time=systime()
}
$2 == 10 && $3 == 20{
line_time=mktime(substr($4,1,4)" "substr($4,7,2)" "substr($4,5,2) " 00 00 00")
if((current_time-line_time)/86400 > thres){ print }
}
' Input_file
Explanation: while starting awk
program and mentioning variable thres
as 30
threshold value posted by OP, could be changed as per need too. Variable in BEGIN
section current_time
which has current system's time in box. In main program, first checking condition if 2nd field is 10 and 3rd field is 20 then move further in current line else go to next line.
Getting 4th field and then using mktime
to change 4th field to epoch time. Then substituting current time with line's time with dividing it with 86400
to convert it to days then checking if its value is greater than threshold then printing current line.
Upvotes: 4
Reputation: 26086
Thinking about it further, I think you may be trying to ask for lines where $2 == 10 and $3 == 20 and the date represented by the 4th column is more than 30 days from now.
If so this probably does the job:
awk -F '|' '$2 == 10 && $3 == 20 { t = mktime(substr($1, 0, 4) " " substr($4, 5, 2) " " substr($4, 7, 2) " 0 0 0") ; now = systime() ; if ( ( (t - now) / 86400) > 30 ) print }'
But, again, don't really know awk.
Upvotes: 0