大文字と小文字を無視し、grepでフレーズの発生回数を計算する方法は?

大文字と小文字を無視し、grepでフレーズの発生回数を計算する方法は?

nicolas bomberで名前を検索する必要がありますgrep。名前も大丈夫ですNiCoLaS BomBer.shその名前の発生回数を示すファイルを実行する必要があります。

私が下したコマンドは次のとおりです。

grep -i "Nicolas \s*Bomber" annuaire | wc -l

しかし、うまくいきません。

どんな提案がありますか?

ベストアンサー1

grepは-o一致するものだけを出力し、行を無視しますwc

grep -io "nicolas bomber" annuaire | wc -l

または簡単に言えば、

grep -ioc "nicolas bomber" annuaire

-zコメントしたように、オプションを使用して単語間のスペースの数を一致させることができます。

grep -iz "nicolas[[:space:]]*bomber" annuaire | wc -l

~から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.

または、すべてのファイルなど、特定のファイル拡張子の文字列を検索するには、次のように*.txt使用できます。

grep -R --include='*.txt' -ioc "nicolas bomber" .

おすすめ記事