我需要搜索nicolas bomber
带有grep
. 名字也可以NiCoLaS BomBer
。我需要运行一个.sh
显示该名称出现次数的文件。
我发出的命令是这样的:
grep -i "Nicolas \s*Bomber" annuaire | wc -l
Run Code Online (Sandbox Code Playgroud)
但它不起作用。
有什么建议?
grep-o
只会输出匹配项,忽略行;wc
可以计算它们:
grep -io "nicolas bomber" annuaire | wc -l
Run Code Online (Sandbox Code Playgroud)
或者干脆,
grep -ioc "nicolas bomber" annuaire
Run Code Online (Sandbox Code Playgroud)
正如您所评论的,您可以使用-z
选项匹配单词之间的任意数量的空格,
grep -iz "nicolas[[:space:]]*bomber" annuaire | wc -l
Run Code Online (Sandbox Code Playgroud)
从 man grep
-i, --ignore-case
Ignore case distinctions in both the PATTERN and the input files. (-i is specified by POSIX.)
-o, --only-matching
Print only the matched (non-empty) parts of a matching line, with each such part on a separate output line.
-c, --count
Suppress normal output; instead print a count of matching lines for each input file.
Run Code Online (Sandbox Code Playgroud)
或者,如果您想搜索特定文件扩展名中的字符串,例如说所有*.txt
文件,您可以使用:
grep -R --include='*.txt' -ioc "nicolas bomber" .
Run Code Online (Sandbox Code Playgroud)