7

Linux コマンドを使用して、文字列を含むファイルの名前だけを一覧表示したいと考えています。

egrep を使用すると、文字列のすべてのインスタンスが表示されますが、これは複数回表示される可能性があります。

例えば:

egrep disco music_file*.txt

表示される場合があります。

music_file1.txt: blah blah blah disco stuff

music_file1.txt: disco blah blah blah

music_file1.txt: I like to listen to italodisco

music_file2.txt: I like to party to disco

music_file3.txt: Does your dog like disco?

私が欲しいのは次のとおりです。

music_file1.txt

music_file2.txt

music_file3.txt

質問: Linux で文字列を検索するときに、各ファイル名のインスタンスを 1 つだけ表示するにはどうすればよいですか?

4

2 に答える 2

11

egrep 式に追加-lします。

egrep -l disco music_file*.txt

からman grep:

-l, --files-with-matches

通常の出力を抑制します。代わりに、出力が通常出力される各入力ファイルの名前を出力します。スキャンは最初の一致で停止します。(-l は POSIX で指定されています。)

于 2013-09-09T09:11:04.130 に答える
3

grep -l がうまくいくはずです。

  grep -l pattern files*.txt

マニュアルページから:

      -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.)
于 2013-09-09T09:12:27.673 に答える