-1

I can find my lines with this pattern, but in some case the info is on the line after the match. How can I also get the line following my match line?

sed -n '/SQL3227W  Record token/p' /log/PLAN_2015-08-16*.MSG >ERRORS.txt
Ed Morton
  • 188,023
  • 17
  • 78
  • 185

2 Answers2

3

Firstly, this looks like a job for grep:

grep -A 1 'SQL3227W  Record token' /log/PLAN_2015-08-16*.MSG >ERRORS.txt

(-A 1 means to print an additional 1 line After the match).

Secondly, if you're using GNU sed, you can use a second address of +1 thus:

sed -n '/SQL3227W  Record token/,+1p' /log/PLAN_2015-08-16*.MSG >ERRORS.txt

Otherwise, (if you really must use non-Gnu sed), then each time you match, append the following line to your pattern space. Delete the first line, before continuing loop (in case the second line is also a match).

Untested code:

#!/bin/sed -nf
/SQL3227W  Record token/{
N
P
D
}
Toby Speight
  • 27,591
  • 48
  • 66
  • 103
0

sed is for simple substitutions on individual lines, that is all. For anything even slightly more interesting just use awk:

awk '/SQL3227W  Record token/{c=2} c&&c--' file

See Printing with sed or awk a line following a matching pattern for other related idioms.

Community
  • 1
  • 1
Ed Morton
  • 188,023
  • 17
  • 78
  • 185