LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-11-2012, 09:58 PM   #1
holyearth
LQ Newbie
 
Registered: Nov 2008
Posts: 5

Rep: Reputation: 0
Combine lines from file


Hello,

Input file looks like this:
apples
bananas
oranges and rice
pears
cherries
mango

I want output to look like this:
apples bananas
oranges and rice pears
cherries mango

It should combine line 1 with line 2 and line 3 with line 4 like that....

Right now, only way I can think is to use head -n 2 but I want output file to be created with all of input processed....

Thanks for all the help in advance!!!
 
Old 08-12-2012, 03:00 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Try this:
Code:
sed 'N;s/\n/ /' infile
The above appends the next line to the buffer, removes the \n and prints the buffer content.
 
Old 08-12-2012, 03:10 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Awk
Code:
awk 'ORS=NR%2?" ":"\n"' file
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Inline perl - combine two lines, need space in between tcxnieman Programming 1 07-03-2012 09:51 AM
[SOLVED] Perl: how to replace blank lines in a file with given lines from another karamaz0v Programming 8 04-19-2012 06:48 AM
how to combine same lines to one in a file eagal Linux - Newbie 22 11-10-2011 08:20 PM
Delete Duplicate Lines in a file, leaving only the unique lines left xmrkite Linux - Software 6 01-14-2010 06:18 PM
How would you combine files excluding the lines that are different? darcman Linux - Software 10 01-19-2009 07:23 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:35 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration