ProgrammingThis 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.
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.
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:
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!
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
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.
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!
I used this example today to combine together the output of a Grep and funnel that output to another program:
Code:
cat LOGFILE | grep -E ".* MUST HAVE THIS TEXT .*" | perl -lpe '($_) = /(Part I'm Interested In)/' | sed -e :a -e N -e 's/\n/ /' -e ta | xargs MY PROGRAM
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.