Operating System - HP-UX
1834935 Members
2494 Online
110071 Solutions
New Discussion

joining consecutive lines

 
Franky Leeuwerck
Frequent Advisor

joining consecutive lines

Hello,

I have file which looks like :
xxxxxxxxxx
xxx pattern xxx
yyy
xxx pattern xxx
yyy
xxxxxxxx

I would like to join consecutive lines so the output looks like :

xxxxxxxxxx
xxx pattern xxxyyy
xxx pattern xxxyyy
xxxxxxxx

How can this be realised with a simple sed or awk statement ?

Thanks in advance.
Franky
8 REPLIES 8
Franky Leeuwerck
Frequent Advisor

Re: joining consecutive lines

Meanwhile I found a solution myself here it is.

sed '/pattern/N;s/\n/ /' filename

This will join only the lines with the pattern.

Thanks anyway.

Franky
Peter Kloetgen
Esteemed Contributor

Re: joining consecutive lines

Hi Franky,

you forgot to assign points to yourself for the quick solution of your problem....

:-)

Allways stay on the bright side of life!

Peter
I'm learning here as well as helping
Franky Leeuwerck
Frequent Advisor

Re: joining consecutive lines

Peter,

Unfortunately you can not assign points for your own solutions. If that would have succeeded, then that would be my first points to be honest.

Regards,
Franky
H.Merijn Brand (procura
Honored Contributor

Re: joining consecutive lines

Give your opinion about my T-Shirt design, and I'll give you your first forum points. Deal?

http://forums.itrc.hp.com/cm/QuestionAnswer/1,,0x3b617bb04b5cd611abdb0090277a778c,00.html
Enjoy, Have FUN! H.Merijn
Franky Leeuwerck
Frequent Advisor

Re: joining consecutive lines

Sorry, the solution was found too soon.

Seems that the problem is a bit more complicated. It is sometimes necessary to join 3 or more lines.

Suppose the file looks like :
xxxxx
xx patternA xx
patternB
xxxxx
xx patternA xx
xxxxx
patternB
xxxxx

The result file should like like :
xxxxx
xx patternA xxpatternB
xxxxx
xx patternA xxxxxxxpatternB
xxxxx

Any suggestions,

Thanks in advance
Franky
Franky Leeuwerck
Frequent Advisor

Re: joining consecutive lines

Well,

I found the solution for the real problem, not on my last question :

cat file.org | sed -e '/startpattern/,/endpattern$/ d' > file.new

This will delete every block (multiple consecutive lines ) in the file starting with a line containing the startpattern and ending with a line ending with the endpattern.

Thanks anyway.
Steven Sim Kok Leong
Honored Contributor

Re: joining consecutive lines

Hi,

Below works on the assumption that "xx patternA" can itself be a static pattern. Apart from that, no problems getting everything from patternA to patternB to be on a the same line:

bash-2.05a# cat tstfile
xxxxx
xx patternA xx
patternB
xxxxx
xx patternA xx
xxxxx
patternB
xxxxx

bash-2.05a# echo `cat tstfile` | sed "s/ xx patternA/;xx patternA/g" | sed "s/patternB /patternB;/g" | tr [";"] ["\n"]
xxxxx
xx patternA xx patternB
xxxxx
xx patternA xx xxxxx patternB
xxxxx

Hope this helps. Regards.

Steven Sim Kok Leong
H.Merijn Brand (procura
Honored Contributor

Re: joining consecutive lines

Is it possible to reverse the problem?

Suppose the file looks like :
xxxxx
xx patternA xx
patternB
xxxxx
xx patternA xx
xxxxx
patternB
xxxxx

The result file should like like :
xxxxx
xx patternA xxpatternB
xxxxx
xx patternA xxxxxxxpatternB
xxxxx

# perl -pe 'BEGIN{$/="xxxxx\n"}chomp;s/\n/ /g;$_.=$/' xx.txt
xxxxx
xx patternA xx patternB xxxxx
xx patternA xx xxxxx
patternB xxxxx

Enjoy, Have FUN! H.Merijn