我有一个包含日志的大型文本文件。执行以下 awk 命令后,我得到以下输出
grep TEST-FIELD test.log | awk '{print $23,$24,$4,$6,$7,$11,$12,$13}'
2020/01/12 08:43:14 TEST-FIELD 10001000 RRRT100 xx.xx.xx.xx [xx-xxxx] xx.xx.xx.xx
2020/01/12 08:43:14 TEST-FIELD 10001000 RRRT100 xx.xx.xx.xx [xx-xxxx] xx.xx.xx.xx
2020/01/12 08:43:14 TEST-FIELD 10001000 RRRT100 xx.xx.xx.xx [xx-xxxx] xx.xx.xx.xx
2020/01/12 08:43:14 TEST-FIELD 10001000 RRRT100 xx.xx.xx.xx [xx-xxxx] xx.xx.xx.xx
我需要将日期列($23)的输出更改为这样
2020-01-12 08:43:14 TEST-FIELD 10001000 RRRT100 xx.xx.xx.xx [xx-xxxx] xx.xx.xx.xx
2020-01-12 08:43:14 TEST-FIELD 10001000 RRRT100 xx.xx.xx.xx [xx-xxxx] xx.xx.xx.xx
2020-01-12 08:43:14 TEST-FIELD 10001000 RRRT100 xx.xx.xx.xx [xx-xxxx] xx.xx.xx.xx
2020-01-12 08:43:14 TEST-FIELD 10001000 RRRT100 xx.xx.xx.xx [xx-xxxx] xx.xx.xx.xx
我尝试执行以下命令,但它根本没有打印正确的值
grep TEST-FIELD test.log | awk -F"/" '{OFS="/"; $23=strftime("%Y-%m-%d", $23); print $24,$4,$6,$7,$11,$12,$13}'
如何仅使用 AWK 获得正确的输出
答案1
将原来的 awk 更改为:
awk '{gsub ("/", "-", $23); print $23,$24,$4,$6,$7,$11,$12,$13}'
答案2
命令
awk '{x=substr($1,1,4)"-"substr($1,6,2)"-"substr($1,9,2);$1="";print x$0}' filename
输出
2020-01-12 08:43:14 TEST-FIELD 10001000 RRRT100 xx.xx.xx.xx [xx-xxxx] xx.xx.xx.xx
2020-01-12 08:43:14 TEST-FIELD 10001000 RRRT100 xx.xx.xx.xx [xx-xxxx] xx.xx.xx.xx
2020-01-12 08:43:14 TEST-FIELD 10001000 RRRT100 xx.xx.xx.xx [xx-xxxx] xx.xx.xx.xx
2020-01-12 08:43:14 TEST-FIELD 10001000 RRRT100 xx.xx.xx.xx [xx-xxxx] xx.xx.xx.xx