使用awk整理日志格式的代码

发布时间:2019-11-21编辑:脚本学堂
使用awk整理日志格式的代码,学习awk的强大之处。

文本如下:
ITEM 1: login screen welcome message|ges RESULT: PASsed
ITEM 2.1: File /etc/rc.dt NOT exists|ges RESULT: PASSED
ITEM 2.2: /usr/bin/X11/xhost permission 744|ges RESULT: PASSED
Item 3: Default user password policy and umask|ges RESULT: PASSED
ITEM 4.1 password policy for system users|ges RESULT: PASSED
ITEM 4.2 password policy for application users|ges RESULT: PASSED
ITEM 4.3 password policy for system admin|ges RESULT: PASSED

整理后的结果:
ges|info|ITEM 1:|login screen welcome message|ges RESULT: PASSED   
ges|info|ITEM 2.1:|File /etc/rc.dt NOT exists|ges RESULT: PASSED   
ges|info|ITEM 2.2:|/usr/bin/X11/xhost permission 744|ges RESULT: PASSED    
ges|info|Item 3:|Default user password policy and umask|ges RESULT: PASSED 
ges|info|ITEM 4.1|password policy for system users|ges RESULT: PASSED  
ges|info|ITEM 4.2|password policy for application users|ges RESULT: PASSED  
ges|info|ITEM 4.3|password policy for system admin|ges RESULT: PASSED 

中间有个判断passed为info,其他为error

执行文件代码如下:
linuxjishu/13830.html target=_blank class=infotextkey>awk '{$2=$2"|";print $NF ~ /PASSED/ ? "ges|info|"$0 : "ges|error|"$0}'