如何计算grep中短语的出现次数,忽略大小写?

Joh*_*nPc 2 shell grep wc

我需要搜索nicolas bomber带有grep. 名字也可以NiCoLaS BomBer。我需要运行一个.sh显示该名称出现次数的文件。

我发出的命令是这样的:

grep -i "Nicolas \s*Bomber" annuaire | wc -l
Run Code Online (Sandbox Code Playgroud)

但它不起作用。

有什么建议?

Rah*_*hul 9

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)