我是个新手。我有一个下面的文件,其中有4列;用管道符号分隔。
test.unl
XCS|10|20|20200505|
AWX|20|10|20200606|
WSX|20|10|20200517|
RFV|20|10|20200520|
TGB|10|20|20200609|
我需要编写一个命令,如果第2列为10,第3列为20,那么从最后一列减去当前日期,如果日期之间的差值大于30,则在文件中打印整行。
我正在运行下面的命令,这会给我带来错误。
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
以下是运行该命令时收到的错误:
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
期待您的帮助,谢谢!
发布于 2020-06-10 12:07:28
编辑:使用awk
的systime
函数,而不是使用date
shell命令,请尝试如下。
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
解释:在启动awk
程序并将变量thres
作为OP发布的30
阈值时,也可以根据需要进行更改。变量在具有当前系统时间的BEGIN
段current_time
中。主程序中,如果第二字段为10,第三字段为20,则先检查条件,然后在当前行中进一步移动,否则转到下一行。得到第四个场,然后用mktime
将第四个场转换为划时代的时间。然后用行的时间代替行的时间,用86400
将其除以,将其转换为天,然后检查其值是否大于阈值,然后打印当前行。
发布于 2020-06-10 12:18:04
进一步考虑一下,我认为您可能想要的行是$2 == 10和$3 == 20以及第4列所代表的日期是30天以上。
如果是这样的话,这很可能就是这样的工作:
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 }'
但是,再说一遍,我真的不知道。
https://stackoverflow.com/questions/62302760
复制相似问题