计算文件中模式的出现次数(即使在同一行上) [英] Count number of occurrences of a pattern in a file (even on same line)
问题描述
当搜索文件中字符串的出现次数时,我通常使用:
grep模式文件| wc -l
但是,由于grep的工作原理,如何搜索字符串在文件中出现的次数,无论它们是在相同或不同的行上?
此外,如果我搜索正则表达式模式,而不是一个简单的字符串?
要计算所有出现次数,请使用 -o
。尝试此操作:
echo afoobarfoobar | grep -o foo |当然,wc -l
和 man grep
(:
更新
有些人建议只使用 grep -co foo
而不是
grep -o foo | wc -l </ code>。
p>
这个快捷方式在所有情况下都无效。页面说明:
-c打印匹配线条数
这些方法的区别如下:
1。
$ echo afoobarfoobar | grep -oc foo
1
一旦在行中找到 $ c> a {foo} barfoobar )搜索停止,只有一行被检查并匹配,所以输出 1
code> -o 在此处被忽略,您可以改用 grep -c
。
2。
$ echo afoobarfoobar | grep -o foo
foo
foo
$ echo afoobarfoobar | grep -o foo | wc -l
2
在行中找到两个匹配( a {foo} bar {foo} bar
),因为我们明确要求查找每个发生( -o
) 。每次发生都打印在单独的行上, wc -l </ code>只是计算输出中的行数。
When searching for number of occurrences of a string in a file, I generally use:
grep pattern file | wc -l
However, this only finds one occurrence per line, because of the way grep works. How can I search for the number of times a string appears in a file, regardless of whether they are on the same or different lines?
Also, what if I'm searching for a regex pattern, not a simple string? How can I count those, or, even better, print each match on a new line?
To count all occurrences, use -o
. Try this:
echo afoobarfoobar | grep -o foo | wc -l
And man grep
of course (:
Update
Some suggest to use just grep -co foo
instead of grep -o foo | wc -l
.
Don't.
This shortcut won't work in all cases. Man page says:
-c print a count of matching lines
Difference in these approaches is illustrated below:
1.
$ echo afoobarfoobar | grep -oc foo
1
As soon as the match is found in the line (a{foo}barfoobar
) the searching stops. Only one line was checked and it matched, so the output is 1
. Actually -o
is ignored here and you could just use grep -c
instead.
2.
$ echo afoobarfoobar | grep -o foo
foo
foo
$ echo afoobarfoobar | grep -o foo | wc -l
2
Two matches are found in the line (a{foo}bar{foo}bar
) because we explicitly asked to find every occurrence (-o
). Every occurence is printed on a separate line, and wc -l
just counts the number of lines in the output.
这篇关于计算文件中模式的出现次数(即使在同一行上)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!