Operating System - HP-UX
1830898 Members
1970 Online
110017 Solutions
New Discussion

sed and joining lines questions

 
SOLVED
Go to solution
Scott G. Gallup
Advisor

sed and joining lines questions

Hello,

I have a text file where I wish to join 3 consecutive lines into one line using sed.

The first line always has the following word (Commercial) as the first word of the line and I want to append the next two lines to create a single line.

I have been able to join two consecutive lines using sed but joining three lines has eluded me.

Any help, suggestions, examples would be greatly appreciated
8 REPLIES 8
James R. Ferguson
Acclaimed Contributor
Solution

Re: sed and joining lines questions

Hi Scott:

Try:

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

Regards!

...JRF...
Chris Vail
Honored Contributor

Re: sed and joining lines questions

James--Thou art the sed master......



Chris
Vitek Pepas
Valued Contributor

Re: sed and joining lines questions

Any ideas how to join all the lines in a file into one line using sed?
James R. Ferguson
Acclaimed Contributor

Re: sed and joining lines questions

Hi Vitek:

In answer to your question:

# echo `cat filename`

...or:

# xargs < filename

Regards!

...JRF...

Ramkumar Devanathan
Honored Contributor

Re: sed and joining lines questions

Scott,

sed '/^Commercial/ N;s/\n/ /;N;s/\n/ /' filename

would be exactly what you are looking for.

- ramd.
HPE Software Rocks!
Scott G. Gallup
Advisor

Re: sed and joining lines questions

James,

Worked like a charm.

Thanks for the quick response.

Can I assume the reply for Vitek is to another posting?
James R. Ferguson
Acclaimed Contributor

Re: sed and joining lines questions

Hi (again) Scott:

The reply to Vitek was in answer to his query *within* this post. :-))

Regards!

...JRF...
Vitek Pepas
Valued Contributor

Re: sed and joining lines questions

James,
Thanks, but your solution adds extra spaces between the lines. Removing them may not be easy if original lines also contained spaces.

Scott,
I'm sorry for using your posting, I should have opened my own ...
Please do me a favor and assign 7 pts for James on my behalf.
Thank you.