
コマンドでgrep
次のように必要なテキストを見つけました。
grep 'C02' ~/temp/log.txt
今、必要な文字列が見つかるたびに、見つかった文字列の後に続く行を印刷したいと思います。
たとえば、必要なテキストが12行にあり、13行でも見つかるとすると、abc
13abc
行も印刷したいと思います。
答え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