[英]Why am I getting different answers from grep -oc “foo” file and grep -o “foo” file | wc -l ? Which is correct?
The question says it all really... I am trying to find the number of occurences of specific strings in a large file. 这个问题确实说明了一切……我正在尝试查找大文件中特定字符串出现的次数。
The answers are similar.... like 50 000 or so with the pipe and 49 000 odd with just the plain grep - oc... 答案是相似的。。。。。。。。。。。。。。。。。。。。。。。。。。
Can anyone explain why I am getting different answers, and which one would be correct? 谁能解释为什么我得到不同的答案,哪个是正确的?
Thanks for the help. 谢谢您的帮助。
grep -c
counts only the matching lines whereas grep -o
prints each matching part on a separate line which means that wc -l
will print more "lines" than actually match. grep -c
仅计算匹配行,而grep -o
每个匹配部分打印在单独的行上,这意味着wc -l
将打印比实际匹配更多的“行”。
As far as I can see, there is no effective difference between grep -oc
and grep -c
because the suppression of printing means that there is no resulting effect of -o
any more. 据我
grep -oc
, grep -oc
和grep -c
之间没有有效的区别,因为抑制打印意味着不再有-o
结果。
For example, printing a three line "file" with one double match and one single match: 例如,打印一个三行“文件”,其中包含一个双匹配和一个单匹配:
$ printf 'foo foo\nbar\nfoo\n' | grep -oc foo
2
$ printf 'foo foo\nbar\nfoo\n' | grep -o foo
foo
foo
foo
In --only-matching
( -o
) mode grep will sometimes output multiple lines for a single matching line. 在
--only-matching
( -o
)模式下,grep有时会为单个匹配行输出多行。
alex@yuzu:~$ echo -e "foo\nbar\nbaz"
foo
bar
baz
alex@yuzu:~$ echo -e "foo\nbar\nbaz" | grep -o o
o
o
But with --count
( -c
) it will count the number of matching lines. 但是使用
--count
( -c
)它将计算匹配的行数。
alex@yuzu:~$ echo -e "foo\nbar\nbaz" | grep -oc o
1
So grep -oo | wc -l
所以
grep -oo | wc -l
grep -oo | wc -l
is counting all the matches, even if there is more than one match in a single line. 即使单行中有多个匹配项,
grep -oo | wc -l
也会计算所有匹配项。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.