1825774 Members
2010 Online
109687 Solutions
New Discussion

Joining lines in a file

 
SOLVED
Go to solution
Dave Walley
Frequent Advisor

Joining lines in a file

Hi.

I have a file with 50 lines, I want to join the second line onto the first the fourth onto the third and so on. Can anybody help please.

Thanks

Dave
why do i do this to myself
6 REPLIES 6
Thierry Poels_1
Honored Contributor

Re: Joining lines in a file

hi,

with "ed":

ed $yourfile << [EOT]
,g/.*/.,.+1j
w
q
[EOT]

Many other solutions available. There will be Perl variants here in no time.

good luck,
Thierry.
All unix flavours are exactly the same . . . . . . . . . . for end users anyway.
Mark Grant
Honored Contributor
Solution

Re: Joining lines in a file

while read one
do
read two
echo "$one $two"
done < filename
Never preceed any demonstration with anything more predictive than "watch this"
Graham Cameron_1
Honored Contributor

Re: Joining lines in a file

awk '{printf "%s", $0;getline;print}' yourfile
Computers make it easier to do a lot of things, but most of the things they make it easier to do don't need to be done.
A. Clay Stephenson
Acclaimed Contributor

Re: Joining lines in a file

Probably the simplest approach is to use 'tr' to translate ASCII linefeeds (octal 012's) to spaces, viz:

tr '\012' ' ' < oldfile > newfile
If it ain't broke, I can fix that.
Mark Grant
Honored Contributor

Re: Joining lines in a file

A. Clay Stephenso

Mightily impressed with your general ability to compress an entire flight controll system into about a three line shell script (particulary loved your loading "bc" as a co-process solution some time ago) but surely this particular solution will output the whole file as one line.

Please let me know if I am mis-reading this.
Never preceed any demonstration with anything more predictive than "watch this"
Tim Sanko
Trusted Contributor

Re: Joining lines in a file

I would grant that you seem to be correct, Mark