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. |
 |
06-08-2004, 12:01 PM
|
#1
|
|
Member
Registered: Jul 2003
Location: Geneva, Switzerland
Distribution: Debian 3.1, SLC3 (based on RHEL)
Posts: 84
|
Merge lines in a file using sed
[ Log in to get rid of this advertisement]
Hi!
I am just new to sed and I am scratching my head to write a script that would take the multiple lines of a file and would merge them together in a single line. I thought sed would be good to do that! For example, here are the few first line of my file:
98e_30788
98e_30802
98e_30825
98e_30849
98e_30888
98e_30903
98e_30923
98e_30943
98e_30956
and I want to get the following output:
98e_30788 98e_30802 98e_30825 ...
I found a script on the web that was merging two specific lines of a file but I just can't make it through the different commands of the script. I have searched for the threads in this forum about sed, but none of them were useful for my problem. If someone has any clue, please help me!

|
|
|
|
06-08-2004, 12:27 PM
|
#2
|
|
Member
Registered: May 2002
Posts: 959
|
Try tr
Code:
cat myfile | tr -d '\n' > newfile
|
|
|
|
06-08-2004, 12:32 PM
|
#3
|
|
Senior Member
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu 'hardy heron'
Posts: 2,219
|
Jim's solution is much better, but if you want to do it the sed way:
Only if the file is not too big, sed can do it. The reason is that sed normally works in a line-by-line way. There is a way around this: sed's N command reads the next line and appends it to its buffer in memory. Then you can have sed delete the newline ('\n') between it by replacing it with a space. Then append the next line, and so on, until the entire file is in sed memory buffer with newlines replaced by spaces. If there's nothing left to read, sed prints the entire buffer to stdout:
Code:
sed -e :a -e N -e 's/\n/ /' -e ta yourfile.txt >newfile.txt
Here's the script line-by-line with comments:
Code:
:a # label 'a' to jump back to later on
N # Append next line to sed's buffer
s/\n/ / # replace exactly one newline (\n) with a space
ta # if the last replace command was succesfull, jump to label 'a'.
# (if this does not happen, the entire file was read: end
Last edited by Hko; 06-08-2004 at 12:34 PM..
|
|
|
|
06-08-2004, 12:41 PM
|
#4
|
|
Member
Registered: Jul 2003
Location: Geneva, Switzerland
Distribution: Debian 3.1, SLC3 (based on RHEL)
Posts: 84
|
Thanks a million guys!
I didn't even know about tr and now I do, with great pleasure :-)
Thanks for the explanation on the sed call! Sed is probably one of the most poorly documented program in the world! So it's always good to not only get the script but also know what each command does.
|
|
|
|
06-09-2004, 08:33 AM
|
#6
|
|
Member
Registered: Jul 2003
Location: Geneva, Switzerland
Distribution: Debian 3.1, SLC3 (based on RHEL)
Posts: 84
|
Quote:
|
I would not agree it's poorly documented
|
Thanks for the links, I will surely look at them!
I was only saying this because my search for documentation was mostly not succesful and the few sites I found were also saying that they had a hard time finding a good manual. That's all! 
|
|
|
|
06-09-2004, 09:05 AM
|
#7
|
|
Senior Member
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu 'hardy heron'
Posts: 2,219
|
OK, no problem
|
|
|
|
05-06-2008, 04:07 PM
|
#8
|
|
LQ Newbie
Registered: May 2008
Posts: 8
|
I have the same problem today and I solve it with:
Translating for your case, we have:
paste -s -d " " input.txt > output.txt
However, I do not know whether it is the best way to do it when input.txt is a very large file.
Kindly,
LJr.
|
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 02:50 PM.
|
|
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
LQ Podcast
LQ Radio
|
|