Linux:grepコマンドで行番号を表示する方法

スポンサーリンク

grepコマンドで行番号を表示する方法

grepコマンドで行番号を表示するには-nオプションを指定します。

 

オプションの説明

  -n, --line-number         print line number with output lines

 

以下のファイルを対象とします。

$ cat test.txt
apple,001
apple,002
banana,003
lemon,004
orange,005
strawberry,006

 

-nオプションを指定した場合

$ grep -n 'apple' test.txt
1:apple,001
2:apple,002

 

-nオプションを指定しない場合

$ grep 'apple' test.txt
apple,001
apple,002