如何grep双引号后跟一个字符串同时?

时间:2017-07-14 12:09:51

标签: shell unix awk grep

   [["QUALITY",**"AVM",200**,610865413,1,0,7,2,-1],[5,0,0,0,0,2],
   [0.998286,0,1,0,0],[0,0,0,0,0,0],["aq04rvtcmgff8"],
   [["QUALITY",**"AVM",200**,610865413,1,0,7,2,-1],[5,0,0,0,0,2],
   [0.998286,0,1,0,0],[0,0,0,0,0,0],["aq04rvtcmgff8"]
   [["QUALITY",**"AVM",200**,610865413,1,0,7,2,-1],[5,0,0,0,0,2],
   [0.998286,0,1,0,0],[0,0,0,0,0,0],["aq04rvtcmgff8"]
   [["QUALITY","AVM”,100,610865413,1,0,7,2,-1]

这是我的日志。我想要模式如下:

**"AVM",200** - In the above log, how many time this pattern comes. Result should be **3** 

我试过了:

**grep "\"AVM\",200" <file> | wc -l** --> but its not giving perfect result 

1 个答案:

答案 0 :(得分:2)

如果每行有一个匹配项,请使用-c选项

grep -c '"AVM",200' log

如果有多个匹配项,则每行打印一个匹配项,然后计算行数(取决于grep实现,而不是全部都有-o选项)

grep -o '"AVM",200' log | wc -l

或使用GNU awk将搜索字符串设置为记录分隔符并使用记录数来计算

awk -v RS='"AVM",200' 'END{print NR-1}' log
# to handle empty input as well
awk -v RS='"AVM",200' 'END{print (NR>1?NR-1:0)}'

或计算每行的出现次数

awk -F'"AVM",200' 'NF{total += NF-1} END{print total+0}' log