grep 搜尋 + 下一行

grep 搜尋 + 下一行

透過grep命令,我找到了我需要的文本,如下:

grep 'C02' ~/temp/log.txt

現在,無論我在哪裡找到所需的字串,我都想列印找到的字串後面的行。

例如,假設所需的文字是abc,並且abc在第 12 行找到,我也想列印第 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

相關內容