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

相关内容