テキストファイル内の単語に一致する行を検索する

テキストファイル内の単語に一致する行を検索する

grepなどを使用して、テキスト文書のどの行がパターンに一致する特定の単語であるかを判断する方法はありますか?ありがとうございます。

ベストアンサー1

-nはい、オプションがありますgrep

~からman grep:

-n, --line-number
              Prefix each line of output with the 1-based line number within its input file.

たとえば、次のファイルがあるとしますfile.txt

this is
foo test
and this is
bar test

電流出力grep -n "test" file.txt

$ grep -n "test" file.txt 
2:foo test
4:bar test

ここで、2と4はパターンが見つかった行番号を表します。

おすすめ記事