grepコマンドで最大マッチ(一致)数を指定する方法
grepコマンドで最大マッチ(一致)数を指定するには-mオプションを指定します。
オプションの説明
-m, --max-count=NUM stop after NUM matches
以下のファイルを対象とします。
$ cat test.txt apple,001 apple,002 banana,003 lemon,004 orange,005 strawberry,006
-mオプションを指定した場合
$ grep -m 1 'apple' test.txt apple,001
-mオプションを指定しない場合
$ grep 'apple' test.txt apple,001 apple,002