52

With the grep command, I found the text I need as follows:

grep 'C02' ~/temp/log.txt

Now, wherever I find the desired string, I would like to print the line following the found string.

For example, let's say that the desired text is abc, and abc is found on line 12, I would like to print line 13 too.

AdminBee
  • 22,803
Mah
  • 521

1 Answers1

62

If you are using Linux system, you can try:

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.

You can use awk also as:

awk '/C02/{print;getline;print}' ~/temp/log.txt
Kamaraj
  • 4,365