LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 08-06-2008, 05:30 PM   #1
Agentrooker
LQ Newbie
 
Registered: Sep 2003
Posts: 9

Rep: Reputation: 5
Appending ends of lines in sed


Suppose I have two text files with the same number of lines. Is there a way for me to use sed to append the lines from one text file to the end of the lines for another? For example:

file "foo" contains:
Apple
Basket
Crispy

file "bar" contains:
pie
ball
rice

And the resulting file "foobar" should be:
Apple pie
Basket ball
Crispy rice

Can anyone tell me how to go about doing this?
 
Old 08-06-2008, 06:02 PM   #2
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,396
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
If I conclude that you want to use sed because it should be a one-liner, I will offer instead a Perl one-liner:
Code:
perl -e 'open(FOO,"foo"); open(BAR,"bar");while($foo=<FOO>){ $bar=<BAR>; chomp $foo; print "$foo $bar";}'
--- rod.
 
Old 08-06-2008, 06:37 PM   #3
kenoshi
Member
 
Registered: Sep 2007
Location: SF Bay Area, CA
Distribution: CentOS, SLES 10+, RHEL 3+, Debian Sarge
Posts: 159

Rep: Reputation: 32
Do you really need to use sed? How's about:

Code:
paste -d\  foo bar > foobar
 
Old 08-06-2008, 09:31 PM   #4
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 333

Rep: Reputation: 141Reputation: 141
This uses GNU sed:-
Code:
sed 'R bar' foo | sed 'N;s/\n/ /' > foobar
 
Old 08-07-2008, 01:19 AM   #5
chakka.lokesh
Member
 
Registered: Mar 2008
Distribution: Ubuntu
Posts: 211

Rep: Reputation: 32
Quote:
Originally Posted by Kenhelm View Post
This uses GNU sed:-
Code:
sed 'R bar' foo | sed 'N;s/\n/ /' > foobar
I tried this

Code:
 sed 'R file2; s/\n//' file1
but it didn't work. I don't know why? can u please tell me why ?

thanks.
 
Old 08-07-2008, 01:39 AM   #6
burschik
Member
 
Registered: Jul 2008
Posts: 159

Rep: Reputation: 31
Code:
paste -d\  foo bar

Last edited by burschik; 08-07-2008 at 01:40 AM. Reason: needs code tags to display correctly
 
Old 08-07-2008, 09:55 AM   #7
Agentrooker
LQ Newbie
 
Registered: Sep 2003
Posts: 9

Original Poster
Rep: Reputation: 5
Thanks everyone. I don't know why I assumed I had to do it with sed, but I wasn't aware of the paste command. That ended up being a pretty elegant solution to my problem. Thanks again.
 
Old 08-07-2008, 11:18 AM   #8
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 333

Rep: Reputation: 141Reputation: 141
Quote:
Originally Posted by chakka.lokesh
I tried this
sed 'R file2; s/\n//' file1
but it didn't work. I don't know why? can u please tell me why ?
On my system the R command takes the full string after it upto the quote as the filename.
So in your example it would be looking for a file named "file2; s/\n//" not one named "file2".

Also the inserted lines are put into the output stream not into the pattern space.
 
Old 08-08-2008, 12:19 AM   #9
chakka.lokesh
Member
 
Registered: Mar 2008
Distribution: Ubuntu
Posts: 211

Rep: Reputation: 32
Quote:
Originally Posted by Kenhelm View Post
On my system the R command takes the full string after it upto the quote as the filename.
So in your example it would be looking for a file named "file2; s/\n//" not one named "file2".

Also the inserted lines are put into the output stream not into the pattern space.
got it.

thx
 
  


Reply

Tags
sed


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
sed - appending text 7stud Linux - Newbie 2 03-01-2007 03:10 PM
sed: appending words residing in separate file mr_scary Programming 3 10-05-2006 02:25 PM
Sed empty lines nc3b Programming 4 08-27-2006 07:37 AM
awk/gawk/sed - read lines from file1, comment out or delete matching lines in file2 rascal84 Linux - General 1 05-24-2006 10:19 AM
Sed(?); Appending a comma-delineated file ice_hockey Linux - General 2 05-27-2005 09:42 AM


All times are GMT -5. The time now is 10:32 AM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration