Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
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.
Anyway, there may be better solutions, but I got it to work this way. You use "N" to append the next line to the one in the buffer, then replace the newline between them with a space, but only if it appears between actual blocks of text. The whole thing is nested in a "t" loop in order to run the process as many times as needed.
As colucix said. sed commands are always applied to the current contents of the pattern buffer. One line is taken a time into this buffer (minus the delimiting newline), the commands in the expression are applied to it, and the modified contents are printed out (unless told not to). Then the contents are cleared and replaced with the next line. address ranges filter which lines get grabbed, but don't otherwise affect the basic processing sequence.
In order to operate on multiple lines at once, and the newline characters between them, you have to tell sed to store more than one line in the pattern buffer at a time. This is accomplished with the N command, and/or use of the hold buffer. "N" tells sed to append the next line of text to the current buffer, separated by a newline. This is the only time there's actually a newline in the buffer to target.
In this specific case you also need some kind of loop or hold buffer action to continue to add new lines until the blank line condition is reached, otherwise it would only process a single newline, then empty the buffer for the next line.
The hold buffer is a separate bit of swap space that can be used for temporary text storage, and there are various commands for appending to/swapping out text with the pattern buffer. sed expressions can get horribly complex with it, and trying to keep straight exactly what it's doing always makes my head hurt.
Read the grymoire link I gave earlier for more details on how it works.
Last edited by David the H.; 02-11-2012 at 04:41 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.