Join lines using sed or awk

Hi,

I have text file that looks like this:

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 :

PATTERNLINE1LINE2
PATTERNLINE1LINE2
PATTERNLINE1LINE2

How can such 3 linies be joined using sed or awk ?

Regards,
Hench

Hi,
Don't believe to use code tag button for code or data samples.
Either, you can try this:

sed -ne '/PATTERN/{N;N;s/\
//g;p;}' file

Regards.

Hello Hench,

Please use code tags for commands/codes/Inputs into your posts as per forum rules, could you please try following and let me know if this helps.

awk '/PATTERN/ && A{print A;A=""} /PATTERN/ || /LINE1/ || /LINE2/{A=A?A $0:$0}'  Input_file

Output will be as follows.

PATTERNLINE1LINE2
PATTERNLINE1LINE2

Thanks,
R. Singh

1 Like

Hi, try:

sed -n '/PATTERN/{N;N;s/\n//gp;}' file

Maybe this awk , though I am still learning so may not be the best :).

awk '/PATTERN/{if (x)print x;x="";}{x=(!x)?$0:x$0;}END{print x;}' file

output:

PATTERNLINE1LINE2
PATTERNLINE1LINE2
PATTERNLINE1LINE2

As @RavinderSingh13 found my script is not desired and also carries over the bla . Sorry for that:)

IMO, above sed solutions will give wrong results if input has LINE3 in place of LINE1 or LINE2 for example.

Below one should work for given input patterns:

sed -ne '/PATTERN/ {N;/LINE1/N;/LINE2/s/\n//gp}' file

Hello cmccabe,

IMHO above code will not produce output as per OP's request. It will give bla also in output.

Thanks,
R. Singh

1 Like

Thank you, I have updated my answer to reflect this. Learning takes practice :).

I'm not sure that LINE1 and LINE2 are also checked.
The goal is just search PATTERN and concatenate the following 2 lines.

Either, another way in awk:

awk 'A==3{print X$0};/PATTERN/ {A=1;X=""};A>=1{++A;A%=4;X=X$0}' file

Regards.

1 Like

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.

1 Like