grep搜索+下一行

Mah*_*Mah 35 grep

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

grep 'C02' ~/temp/log.txt 现在,无论我在哪里找到所需的字符串,我都想打印找到的字符串后面的行。

例如,假设所需的文本是“abc”,并且在第 12 行找到 abc,我也想打印第 13 行。

小智 43

如果您使用的是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.
Run Code Online (Sandbox Code Playgroud)

您也可以将 awk 用作:

awk '/C02/{print;getline;print}' ~/temp/log.txt
Run Code Online (Sandbox Code Playgroud)

  • 另外,`sed -n '/C02/{N; p}' ~/temp/log.txt` (2认同)