1つの文字列を含むが他の文字列は含まないファイルを見つける

1つの文字列を含むが他の文字列は含まないファイルを見つける

私は.txt多くのファイルが含まれているフォルダにあり、含まれているがstringA含まれていないすべてのファイルを探したいと思いますstringB(同じ行にある必要はありません)。これを行う方法を知っている人はいますか?

ベストアンサー1

ファイル名にスペース、タブ、改行(変更されていないと仮定$IFS)、またはワイルドカードが含まれていないで始まらない限り、そのオプションをサポートしている-場合は、次のようにできます。grep-L

$ cat file1
stringA
stringC
$ cat file2
stringA
stringB
$ grep -L stringB $(grep -l stringA file?)
file1

grepサブシェルで実行すると、$()すべてのインクルードが印刷されますstringA。ファイルリストは、grepすべてのインクルードがリストされていない基本コマンドへの入力ですstringB

~からman grep

  -v, --invert-match
          Invert the sense of matching, to select non-matching lines.  (-v is specified by POSIX.)
  -L, --files-without-match
          Suppress normal output; instead print the name of each input file from which no output would normally have been printed.  The scanning will stop on the first match.
  -l, --files-with-matches
          Suppress normal output; instead print the name of each input file from which output would normally have been printed.  The scanning will stop on the first match.  (-l is specified by POSIX.)

おすすめ記事