我如何获得GNU grep完全匹配“H”而不是刚刚以“H”开头的东西? [英] How do I get GNU grep to match exactly "H" and not things that just start with "H"?
问题描述
我有一个文件,我希望找到传入单词的总次数,同时支持正则表达式。
grep -eHello *filename | wc -w
但是有一些错误,我可以说我做了类似于
的 grep -eHfilename | wc -w
它应该只与完全匹配并且不计算以H开头的内容,grep的方式现在就做。
任何人都知道该怎么做? 试试这个:
grep'\bH\b'
例如:
$ $ p $ kent $ echo您好
IamH
我们| grep'\bH\b'
我们需要这个H
和这个H
请注意,如果您只想计算匹配的单词,则需要在 -o
选项上使用的grep
。 (thx fotanus)
编辑
您可以通过 grep -o
,在这种情况下 -c
不起作用,因为它计算了匹配的行数。您可以将 grep -o
传递给 wc -l </ code>
例如:
kent $ echo没有Hfoo会计算在这行
这行有很多:HHHHHHH( 7)
H(8开始)
foo bar(9结尾)H
H| grep -o'\bH\b'| wc -l
10 $使用awk :b $ b
或更简单的单进程解决方案
: awk'{s + = gsub(/ \< H \> /,)} END {print s}'file
同样的例子:
kent $ echo没有Hfoo将被计入这一行
这行有很多:HHHHHHH(7)
H(8首)
foo bar(9结尾)H
H| awk'{s + = gsub(/ \< H \> /,)} END {print s}'
10
I have a file and I would like to do find the number of total number of occurrences of a passed in word, while supporting regex
grep -e "Hello*" filename | wc -w
But there a few bugs, I let's say I do something like
grep -e "H" filename | wc -w
It should only match EXACTLY H and not count things that start with H, the way grep does it right now.
Anyone know how?
try this:
grep '\bH\b'
e.g.:
kent$ echo "Hello
IamH
we need this H
and this H too"|grep '\bH\b'
we need this H
and this H too
Note that if you want to count only the matched words, you need to use -o
option on grep
. (thx fotanus)
EDIT
You can get all matched words by grep -o
, in this case -c
doesn't help, because it counts matched lines. you could pass grep -o
to wc -l
for example:
kent$ echo "No Hfoo will be counted this line
this line has many: H H H H H H H (7)
H (8 starting)
foo bar (9 ending) H
H"|grep -o '\bH\b'|wc -l
10
or simpler, single process solution with awk:
awk '{s+=gsub(/\<H\>/,"")}END{print s}' file
same example:
kent$ echo "No Hfoo will be counted this line
this line has many: H H H H H H H (7)
H (8 starting)
foo bar (9 ending) H
H"|awk '{s+=gsub(/\<H\>/,"")}END{print s}'
10
这篇关于我如何获得GNU grep完全匹配“H”而不是刚刚以“H”开头的东西?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!