awk_tips
-
简单统计
- 目标:过滤i.txt中的空行、输出错误(非OK结束)的行、并统计错误行数量以exitstatus输出:
-
i.txt范本:
physicaldrive 1I:1:1 (port 1I:box 1:bay 1, 146 GB): OK physicaldrive 1I:1:2 (port 1I:box 1:bay 2, 146 GB): Failed
-
处理命令
cat i.txt |awk 'BEGIN{err=0;}{if($0~/.*physicaldrive.*/){if($NF!="OK"){print "Failed:"$0;err++;}}}END{exit err;}' # 错误记数在退出状态里面: echo $?