我是首发。我有一个下面的文件,其中有 4 列;用管道标志隔开。
test.unl
XCS|10|20|20200505|
AWX|20|10|20200606|
WSX|20|10|20200517|
RFV|20|10|20200520|
TGB|10|20|20200609|
Run Code Online (Sandbox Code Playgroud)
我需要写一个命令,如果第 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
Run Code Online (Sandbox Code Playgroud)
以下是我在运行命令时收到的错误:
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
Run Code Online (Sandbox Code Playgroud)
期待您的帮助,谢谢!
编辑:使用systime函数awk而不是使用dateshell 命令请尝试以下操作。
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
Run Code Online (Sandbox Code Playgroud)
说明:在启动awk程序并提到变量thres作为30OP发布的阈值时,也可以根据需要进行更改。在方框中包含当前系统时间的BEGIN部分current_time中的变量。在主程序中,首先检查条件是否第二场为 10,第三场为 20,然后在当前行中进一步移动,否则转到下一行。获取第 4 个字段,然后mktime用于将第 4 个字段更改为纪元时间。然后用线的时间代替当前时间,将其除以将其86400转换为天,然后检查其值是否大于阈值,然后打印当前行。