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? |
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";}' |
Do you really need to use sed? How's about:
Code:
paste -d\ foo bar > foobar |
This uses GNU sed:-
Code:
sed 'R bar' foo | sed 'N;s/\n/ /' > foobar |
Quote:
Code:
sed 'R file2; s/\n//' file1 thanks. |
Code:
paste -d\ foo bar |
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.
|
Quote:
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. |
Quote:
thx |
All times are GMT -5. The time now is 03:05 AM. |