我正在编写一个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
}
目标:查找代码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 - GCO\johnsmith [1178]dele /P0016/pCR37558666_0.pdf - 250 - - - 9341
4/13/2016 5:00:00 192.168.33.118 - GCO\johnsmith [1190]user GCO\johnsmith - 331 - - - 9342
4/13/2016 5:00:00 192.168.33.118 - GCO\johnsmith [1190]pass ******* - 221 - - - 9342
4/13/2016 5:00:00 192.168.33.118 - GCO\johnsmith [1178]sent /P0016/pCR37558672_0.pdf - 226 - 14141 - 9341
4/13/2016 5:00:00 192.168.33.118 - GCO\johnsmith [1178]dele /P0016/pCR37558672_0.pdf - 250 - - - 9341
4/13/2016 5:00:00 192.168.33.118 - GCO\johnsmith [1178]sent /P0016/pCR37558678_0.pdf - 226 - 14800 - 9341
4/13/2016 5:00:00 192.168.33.118 - GCO\johnsmith [1178]dele /P0016/pCR37558678_0.pdf - 250 - - - 9341
4/13/2016 5:00:00 192.168.33.118 - GCO\johnsmith [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 - GCO\johnsmith [1178]sent /P0016/pCR37558682_0.pdf - 226 - 14132 - 9341
4/13/2016 5:00:00 192.168.33.118 - GCO\johnsmith [1178]dele /P0016/pCR37558682_0.pdf - 250 - - - 9341
4/13/2016 5:00:00 192.168.33.118 - GCO\johnsmith [1178]sent /P0016/pCR37558690_0.pdf - 226 - 14128 - 9341
4/13/2016 5:00:00 192.168.33.118 - GCO\johnsmith [1178]dele /P0016/pCR37558690_0.pdf - 511 - - - 9341
4/13/2016 5:00:00 192.168.33.118 - GCO\johnsmith [1178]sent /P0016/pCR37558694_0.pdf - 226 - 14168 - 9341
4/13/2016 5:00:00 192.168.33.118 - GCO\johnsmith [1178]dele /P0016/pCR37558694_0.pdf - 250 - - - 9341
4/13/2016 5:00:00 192.168.33.118 - GCO\johnsmith [1178]sent /P0016/pCR37558696_0.pdf - 226 - 14170 - 9341
4/13/2016 5:00:00 192.168.33.118 - GCO\johnsmith [1178]dele /P0016/pCR37558696_0.pdf - 250 - - - 9341
4/13/2016 5:00:00 192.168.33.118 - GCO\johnsmith [1178]sent /P0016/pCR37558700_0.pdf - 226 - 14176 - 9341
4/13/2016 5:00:00 192.168.33.118 - GCO\johnsmith [1178]dele /P0016/pCR37558700_0.pdf - 250 - - - 9341
4/13/2016 5:00:00 192.168.33.118 - GCO\johnsmith [1175]user GCO\johnsmith - 221 - - - 9340
4/13/2016 5:00:00 192.168.33.118 - GCO\johnsmith [1175]pass ******* - 230 - - - 9340
4/13/2016 5:00:00 192.168.33.118 - GCO\stmap [1752]user GCO\stmasftp - 331 - - - 8221
4/13/2016 5:00:00 192.168.33.118 - GCO\stmap [1752]pass ******* - 230 - - - 8221
4/13/2016 5:00:00 192.168.33.118 - GCO\johnsmith [1178]sent /P0016/pCR37558704_0.pdf - 226 - 32898 - 9341
4/13/2016 5:00:00 192.168.33.118 - GCO\johnsmith [1178]dele /P0016/pCR37558704_0.pdf - 250 - - - 9341
4/13/2016 5:00:01 192.168.33.118 - GCO\johnsmith [1178]sent /P0016/pCR37558706_0.pdf - 226 - 14111 - 9341
4/13/2016 5:00:01 192.168.33.118 - GCO\johnsmith [1178]dele /P0016/pCR37558706_0.pdf - 250 - - - 9341
4/13/2016 5:00:01 192.168.33.118 - GCO\chubbs [186982]user GCO\chwbsftpsvc - 331 - - - 8234
4/13/2016 5:00:01 192.168.33.118 - GCO\chubbs [186982]pass ******* - 230 - - - 8234
4/13/2016 5:00:01 192.168.33.118 - GCO\johnsmith [1178]sent /P0016/pCR37558708_0.pdf - 226 - 14918 - 9341
4/13/2016 5:00:01 192.168.33.118 - GCO\johnsmith [1178]dele /P0016/pCR37558708_0.pdf - 250 - - - 9341
4/13/2016 5:00:01 192.168.33.118 - GCO\johnsmith [1175]sent /P0018/PCR38150024_0.pdf - 226 - 19845 - 9340
4/13/2016 5:00:01 192.168.33.118 - GCO\johnsmith [1175]dele /P0018/PCR38150024_0.pdf - 250 - - - 9340
4/13/2016 5:00:01 192.168.33.118 - GCO\johnsmith [1178]sent /P0016/pCR37558710_0.pdf - 521 - 14198 - 9341
4/13/2016 5:00:01 192.168.33.118 - GCO\johnsmith [1178]dele /P0016/pCR37558710_0.pdf - 250 - - - 9341
4/13/2016 5:00:01 192.168.33.118 - GCO\johnsmith [1175]sent /P0018/PCR38150026_0.pdf - 226 - 25546 - 9340
以下两个awk语句的结果:
`断开连接(){
断开连接= $(awk' $ 9~ / ^(421 | 221 | 511)$ / {c ++} END {print c}' $ {source_file}) echo"断开连接超时或断开连接" VerifyExitCode
}`
和
`断开连接(){
断开= $(awk' $ 9 ==" 421" || $ 9 ==" 221" || $ 9 ==" 511" { count ++} END {print count}' $ {source_file}) echo"断开连接超时或断开连接" 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。
谢谢!
答案 0 :(得分:2)
计算给定条件的行数
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 - GCO\johnsmith [1190]pass ******* - 221 - - - 9342
4/13/2016 5:00:00 192.168.33.118 - GCO\johnsmith [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 - GCO\johnsmith [1178]dele /P0016/pCR37558690_0.pdf - 511 - - - 9341
4/13/2016 5:00:00 192.168.33.118 - GCO\johnsmith [1175]user GCO\johnsmith - 221 - - - 9340