grep 検索 + 次の行

grep 検索 + 次の行

このgrepコマンドを使用すると、必要なテキストが次のように見つかりました。

grep 'C02' ~/temp/log.txt

ここで、目的の文字列が見つかった場所に、見つかった文字列に続く行を印刷したいと思います。

たとえば、目的のテキストが でabcabc12 行目にある場合、13 行目も印刷したいとします。

答え1

Linux システムを使用している場合は、以下を試してください。

grep -A1 "C02" ~/temp/log.txt


OPTIONS
       -A NUM, --after-context=NUM
              Print NUM lines of trailing context after matching lines.  Places a line containing -- between contiguous groups of matches.
       -B NUM, --before-context=NUM
              Print NUM lines of leading context before matching lines.  Places a line containing -- between contiguous groups of matches.
       -C NUM, --context=NUM
              Print NUM lines of output context.  Places a line containing -- between contiguous groups of matches.

awk は次のようにも使用できます:

awk '/C02/{print;getline;print}' ~/temp/log.txt

関連情報