2013-04-04 34 views
-1

我想搜索在Unix的精确字纹的出现次数,司令部的Unix计算特定字词

例子:log.txt文件包含以下内容:

aaa   (only this 'aaa' pattern shhold be counted) 
bbb 
cccaaa ---> this should not be counted in grep output 
ccc_aaa --> this should not be counted in grep output 
ccc-aaa --> this should not be counted in grep output 
ccc.aaa ---> this should not be counted in grep output 

我使用以下代码 -

count=$? 
count=$(grep -c -w aaa $ZZZ\Log.txt) 

输出。这里应该==> 1,但即时得到4作为输出,我认为,事情是这样丢失,可以在任何一个可以帮助我在这吗?

+0

如果要匹配* only * aaa,请使用^ aaa $作为模式。 – 2013-04-04 06:51:26

+0

Squiguy,Kelvin:非常感谢您的答复 – 2013-04-04 08:01:00

回答

1

我相信你正在寻找-x选项。以下是手册页的摘录,它总是找到选项的最快捷方式。

-x, --line-regexp 
      Select only those matches that exactly match the whole line. 
      (-x is specified by POSIX.) 
0

鉴于样本输入,我希望可以将输出为3.一个你所期望的线路,一个用于ccc-aaa,一个用于ccc.aaa。 grep文档明确指出,单词字符是字母,数字和下划线。如果你要考虑.-为字组成的字符,只是预过滤器中的数据:以上

count=$(tr < $ZZZ/Log.txt .- ' ' | grep -c -w aaa) 

使用tr.-事件转换为空格。根据您的需要,您可能需要扩展想要考虑的字符集。