我正在编写一个bash解析器,其中包含以下函数之一。我试图在第11列中搜索any of the following
类逻辑的所有代码。
Disconnected () {
disconnections=$(awk '$9=="421" & $9=="221" & $9=="511" {print}' ${source_file} | sed 's/[^0-9]*/1/g' | wc -l)
echo "there have been $disconnections timeouts or disconnects"
VerifyExitCode
}
我也试过:
Disconnected () {
disconnections=$(awk '$9=="421" || $9=="221" || $9=="511" {print}' ${source_file} | sed 's/[^0-9]*/1/g' | wc -l)
echo "there have been $disconnections timeouts or disconnects"
VerifyExitCode
}
GOAL:查找出现代码421、221和511的所有事件的总和。因此,如果有12行出现421,10行出现221,3行出现521,它应该打印出there have been 25 timeouts or disconnects
。
我不明白为什么没有把它们加起来。一个给我一个语法错误,另一个给了我减去这些次数的总数。(这种精确的编码适用于单个$disconnect
代码,只是不是3!!)
我试过看man awk,在stackexchange上也试过一些类似的问题,但找不到答案。不过,我确信你们中的一位大师需要大约30秒才能找到答案。谢谢
编辑-添加示例输入日志。
4/13/2016 5:00:00 192.168.33.118 - GCOjohnsmith [1178]dele /P0016/pCR37558666_0.pdf - 250 - - - 9341
4/13/2016 5:00:00 192.168.33.118 - GCOjohnsmith [1190]user GCOjohnsmith - 331 - - - 9342
4/13/2016 5:00:00 192.168.33.118 - GCOjohnsmith [1190]pass ******* - 221 - - - 9342
4/13/2016 5:00:00 192.168.33.118 - GCOjohnsmith [1178]sent /P0016/pCR37558672_0.pdf - 226 - 14141 - 9341
4/13/2016 5:00:00 192.168.33.118 - GCOjohnsmith [1178]dele /P0016/pCR37558672_0.pdf - 250 - - - 9341
4/13/2016 5:00:00 192.168.33.118 - GCOjohnsmith [1178]sent /P0016/pCR37558678_0.pdf - 226 - 14800 - 9341
4/13/2016 5:00:00 192.168.33.118 - GCOjohnsmith [1178]dele /P0016/pCR37558678_0.pdf - 250 - - - 9341
4/13/2016 5:00:00 192.168.33.118 - GCOjohnsmith [1190]quit - - 421 - - - 9342
4/13/2016 5:00:00 192.168.33.118 - - [1190]ssh_disconnect disconnect_by_application - 511 - - - 9342
4/13/2016 5:00:00 192.168.33.118 - GCOjohnsmith [1178]sent /P0016/pCR37558682_0.pdf - 226 - 14132 - 9341
4/13/2016 5:00:00 192.168.33.118 - GCOjohnsmith [1178]dele /P0016/pCR37558682_0.pdf - 250 - - - 9341
4/13/2016 5:00:00 192.168.33.118 - GCOjohnsmith [1178]sent /P0016/pCR37558690_0.pdf - 226 - 14128 - 9341
4/13/2016 5:00:00 192.168.33.118 - GCOjohnsmith [1178]dele /P0016/pCR37558690_0.pdf - 511 - - - 9341
4/13/2016 5:00:00 192.168.33.118 - GCOjohnsmith [1178]sent /P0016/pCR37558694_0.pdf - 226 - 14168 - 9341
4/13/2016 5:00:00 192.168.33.118 - GCOjohnsmith [1178]dele /P0016/pCR37558694_0.pdf - 250 - - - 9341
4/13/2016 5:00:00 192.168.33.118 - GCOjohnsmith [1178]sent /P0016/pCR37558696_0.pdf - 226 - 14170 - 9341
4/13/2016 5:00:00 192.168.33.118 - GCOjohnsmith [1178]dele /P0016/pCR37558696_0.pdf - 250 - - - 9341
4/13/2016 5:00:00 192.168.33.118 - GCOjohnsmith [1178]sent /P0016/pCR37558700_0.pdf - 226 - 14176 - 9341
4/13/2016 5:00:00 192.168.33.118 - GCOjohnsmith [1178]dele /P0016/pCR37558700_0.pdf - 250 - - - 9341
4/13/2016 5:00:00 192.168.33.118 - GCOjohnsmith [1175]user GCOjohnsmith - 221 - - - 9340
4/13/2016 5:00:00 192.168.33.118 - GCOjohnsmith [1175]pass ******* - 230 - - - 9340
4/13/2016 5:00:00 192.168.33.118 - GCOstmap [1752]user GCOstmasftp - 331 - - - 8221
4/13/2016 5:00:00 192.168.33.118 - GCOstmap [1752]pass ******* - 230 - - - 8221
4/13/2016 5:00:00 192.168.33.118 - GCOjohnsmith [1178]sent /P0016/pCR37558704_0.pdf - 226 - 32898 - 9341
4/13/2016 5:00:00 192.168.33.118 - GCOjohnsmith [1178]dele /P0016/pCR37558704_0.pdf - 250 - - - 9341
4/13/2016 5:00:01 192.168.33.118 - GCOjohnsmith [1178]sent /P0016/pCR37558706_0.pdf - 226 - 14111 - 9341
4/13/2016 5:00:01 192.168.33.118 - GCOjohnsmith [1178]dele /P0016/pCR37558706_0.pdf - 250 - - - 9341
4/13/2016 5:00:01 192.168.33.118 - GCOchubbs [186982]user GCOchwbsftpsvc - 331 - - - 8234
4/13/2016 5:00:01 192.168.33.118 - GCOchubbs [186982]pass ******* - 230 - - - 8234
4/13/2016 5:00:01 192.168.33.118 - GCOjohnsmith [1178]sent /P0016/pCR37558708_0.pdf - 226 - 14918 - 9341
4/13/2016 5:00:01 192.168.33.118 - GCOjohnsmith [1178]dele /P0016/pCR37558708_0.pdf - 250 - - - 9341
4/13/2016 5:00:01 192.168.33.118 - GCOjohnsmith [1175]sent /P0018/PCR38150024_0.pdf - 226 - 19845 - 9340
4/13/2016 5:00:01 192.168.33.118 - GCOjohnsmith [1175]dele /P0018/PCR38150024_0.pdf - 250 - - - 9340
4/13/2016 5:00:01 192.168.33.118 - GCOjohnsmith [1178]sent /P0016/pCR37558710_0.pdf - 521 - 14198 - 9341
4/13/2016 5:00:01 192.168.33.118 - GCOjohnsmith [1178]dele /P0016/pCR37558710_0.pdf - 250 - - - 9341
4/13/2016 5:00:01 192.168.33.118 - GCOjohnsmith [1175]sent /P0018/PCR38150026_0.pdf - 226 - 25546 - 9340
下面两个awk语句的结果:
`断开连接(){
断开连接=$(awk'$9~/^(421|221|511)$/{c++}END{print c}'${source_file})echo"存在$disconnections超时或断开连接"VerifyExitCode
}`
和
`断开连接(){
断开连接=$echo"存在$disconnections超时或断开连接"VerifyExitCode
}`
模型是(运行bash-x选项):
Disconnected
++ awk '$9 ~ /^(421|221|511)$/{c++} END{print c}' /root/testlog
+ disconnections=5
+ echo 'there have been 5 timeouts or disconnects'
there have been 5 timeouts or disconnects
+ VerifyExitCode
+ local EXITCODE=0
+ '[' 0 '!=' 0 ']'
这应该是4。
谢谢!
计算给定条件的行数
awk '$9=="421" || $9=="221" || $9=="511" {count++}
END {print count}' file
你也可以写得更紧凑
awk '$9 ~ /^(421|221|511)$/{c++} END{print c}' file
当我在你的样品输入上尝试这个时
$ awk '$9 ~ /^(421|221|511)$/{c++} END{print c}' file
5
检查哪些线路匹配
$ awk '$9 ~ /^(421|221|511)$/' file
4/13/2016 5:00:00 192.168.33.118 - GCOjohnsmith [1190]pass ******* - 221 - - - 9342
4/13/2016 5:00:00 192.168.33.118 - GCOjohnsmith [1190]quit - - 421 - - - 9342
4/13/2016 5:00:00 192.168.33.118 - - [1190]ssh_disconnect disconnect_by_application - 511 - - - 9342
4/13/2016 5:00:00 192.168.33.118 - GCOjohnsmith [1178]dele /P0016/pCR37558690_0.pdf - 511 - - - 9341
4/13/2016 5:00:00 192.168.33.118 - GCOjohnsmith [1175]user GCOjohnsmith - 221 - - - 9340