问题描述
这个问题真的说明了一切......我试图找到特定的出现次数大文件中的字符串.
答案是相似的....像 50 000 左右的管道和 49 000 奇数,只有普通的 grep - oc...
The question says it all really... I am trying to find the number of occurences of specificstrings 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?
感谢您的帮助.
推荐答案
在 --only-matching
(-o
) 模式下 grep 有时会输出多行单匹配行.
In --only-matching
(-o
) mode grep will sometimes output multiple lines for a single matching line.
alex@yuzu:~$ echo -e "foo
bar
baz"
foo
bar
baz
alex@yuzu:~$ echo -e "foo
bar
baz" | grep -o o
o
o
但是使用 --count
(-c
) 它将计算匹配行的数量.
But with --count
(-c
) it will count the number of matching lines.
alex@yuzu:~$ echo -e "foo
bar
baz" | grep -oc o
1
所以 grep -o o |wc -l
正在计算所有匹配项,即使一行中有多个匹配项.
So grep -o o | wc -l
is counting all the matches, even if there is more than one match in a single line.
这篇关于为什么我从 grep -oc "foo" 得到不同的答案?文件和 grep -o "foo";档案 |wc -l ?哪个是对的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!