我想使用BASH将日志文件的输出格式化为单行:
日志输出:
# egrep "^(Infected|Cleaned)" /var/log/scan.log
Infected: files - 0, objects 0
Cleaned: files - 0, objects 0
我能够在一行打印结果,但我不确定如何格式化并删除空格:
# egrep "^(Infected|Cleaned)" /var/log/scan.log | awk '/Infected/{if (x) print x; x="" } { x = (!x) ? $0 : x "; " $0 } END { print x }'
Infected: files - 0, objects 0; Cleaned: files - 0, objects 0
所需格式:
Infected:F-0, O-0; Cleaned:F-0, O-0"
答案 0 :(得分:1)
可怕的快速和肮脏:
egrep "^(Infected|Cleaned)" /var/log/scan.log | awk '/Infected/{if (x) print x; x="" } { x = (!x) ? $0 : x "; " $0 } END { print x }' | sed 's/\s//g' | sed 's/files/F/g' | sed 's/objects/ O-/g' | sed 's/;/; /g'
答案 1 :(得分:1)
我已将输出传输到perl one-liner,这应该这样做
| perl -ne 's/\s+files\s+\-\s+/ F-/g; s/\s+objects\s+/ O-/g; print'
答案 2 :(得分:1)
清洁
awk '/Infected|Cleaned/{ORS=";"; print $1 "F-"$4 "O-"$6}' inputfile
将产生输出
Infected:F-0,O-0;Cleaned:F-0,O-0;
ORS
输出记录分隔符设置为;
,它分隔了不同的行。
/Infected|Cleaned/
选择与模式匹配的行
修改
添加跟踪换行符
awk '/Infected|Cleaned/{ORS=";"; print $1 "F-"$4 "O-"$6}END{print "\n"}'
OR
awk '/Infected|Cleaned/{ORS=";"; print $1 "F-"$4 "O-"$6}' sam; echo
答案 3 :(得分:1)
尝试:
awk 'BEGIN { FS = " +"; ORS = " " }; { print $1, "F-", $4, "O-", $6 }' /var/log/scan.log
FS:字段分隔符,设置为一个或多个空格
ORS:记录输出的分隔符,设置为空格
使用此FS值,所需字段为1,4,6;将包括尾随分隔符(":",",",&#34 ;;",resp。)。