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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
08-06-2008, 04:30 PM
|
#1
|
LQ Newbie
Registered: Sep 2003
Posts: 9
Rep:
|
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?
|
|
|
08-06-2008, 05:02 PM
|
#2
|
LQ 5k Club
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
|
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.
|
|
|
08-06-2008, 05:37 PM
|
#3
|
Member
Registered: Sep 2007
Location: SF Bay Area, CA
Distribution: CentOS, SLES 10+, RHEL 3+, Debian Sarge
Posts: 159
Rep:
|
Do you really need to use sed? How's about :
Code:
paste -d\ foo bar > foobar
|
|
|
08-06-2008, 08:31 PM
|
#4
|
Member
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 360
Rep: 
|
This uses GNU sed:-
Code:
sed 'R bar' foo | sed 'N;s/\n/ /' > foobar
|
|
|
08-07-2008, 12:19 AM
|
#5
|
Member
Registered: Mar 2008
Distribution: Ubuntu
Posts: 270
Rep:
|
Quote:
Originally Posted by Kenhelm
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.
|
|
|
08-07-2008, 12:39 AM
|
#6
|
Member
Registered: Jul 2008
Posts: 159
Rep:
|
Last edited by burschik; 08-07-2008 at 12:40 AM.
Reason: needs code tags to display correctly
|
|
|
08-07-2008, 08:55 AM
|
#7
|
LQ Newbie
Registered: Sep 2003
Posts: 9
Original Poster
Rep:
|
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.
|
|
|
08-07-2008, 10:18 AM
|
#8
|
Member
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 360
Rep: 
|
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.
|
|
|
08-07-2008, 11:19 PM
|
#9
|
Member
Registered: Mar 2008
Distribution: Ubuntu
Posts: 270
Rep:
|
Quote:
Originally Posted by Kenhelm
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
|
|
|
All times are GMT -5. The time now is 11:22 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|