为什么我从grep -oc“foo”中得到不同的答案? file和grep -o“foo”文件| wc -l?哪个是对的? [英] Why am I getting different answers from grep -oc "foo" file and grep -o "foo" file | wc -l ? Which is correct?

查看:223
本文介绍了为什么我从grep -oc“foo”中得到不同的答案? file和grep -o“foo”文件| wc -l?哪个是对的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题说明了这一切......我试图在一个大文件中找到特定
字符串的出现次数。
答案是相似的......就像50 000左右的管道和49 000奇数只是简单的grep - oc ...

任何人都可以解释为什么我得到不同的答案,哪一个会是正确?



感谢您的帮助。

解决方案

c $ c> - 只匹配( -o )mode grep有时会为单个匹配行输出多行。

  alex @ yuzu:〜$ echo -efoo\\\
bar\\\
baz
foo
bar
baz

alex @ yuzu:〜$ echo -efoo\\\
bar\\\
baz| grep -oo
o
o

但是 - 计数 -c )它会计算匹配行数。

  alex @ yuzu:〜$ echo -efoo\\\
bar\\\
baz| grep -oc o
1

所以 grep -o o | wc -l <​​/ code>会计算所有匹配项,即使在一行中有多个匹配项。


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.

解决方案

In --only-matching (-o) mode grep will sometimes output multiple lines for a single matching line.

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.

alex@yuzu:~$ echo -e "foo\nbar\nbaz" | grep -oc o
1

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”中得到不同的答案? file和grep -o“foo”文件| wc -l?哪个是对的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆