blabla
bla
PATTERN
LINE1
LINE2
bla
bla
bla
PATTERN
LINE1
LINE2
bla
PATTERN
LINE1
LINE2
bla
bla
...
There are exactly 2 lines following PATTERN.
I need output to be one line with PATTERN, LINE1 and LINE2 concatenated.
That is, output from input file above should be :
If the OP meant as his requirement to test for those LINES2 too, then this would not work in case PATTERN is followed by LINE2.
to test for the right order, and to ensure the pattern and lines are there, one could do something like this:
sed -n '/PATTERN/{N;N;/\n.*LINE1.*\n.*LINE2/s/\n//gp;}' file
Still a little bit better would be:
sed -n '/PATTERN/{$!N;$!N;/LINE1.*\n.*LINE2/s/\n//gp;}' file
--
Hi Ravinder, this one will not print the last pattern. You need an END section for that and you could replace /PATTERN/ || /LINE1/ || /LINE2/ with /PATTERN|LINE1|LINE2/ .
Also note that is not testing for all three lines to be present as it will still print lines if one or two lines are missing, or in the wrong order, or not adjacent.