Computing.Net > Forums > Unix > Remove all \n in sed

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Remove all \n in sed

Reply to Message Icon

Name: nmf
Date: May 6, 2006 at 16:34:38 Pacific
OS: mac
CPU/Ram: emac
Product: apple
Comment:

I currently have an awk that removes all new lines and
works fine, but I'd like to do it in a sed file since everything else is happeing in the sed file.
in the sed file I have

$!N;s/\n//

but it only removes every other new line.



Sponsored Link
Ads by Google

Response Number 1
Name: James Boothe
Date: June 8, 2006 at 12:07:30 Pacific
Reply:

Yes, your solution pulls line2 into the buffer with line1. With two lines in the buffer, sed can "see" and operate on the embedded newline that separates the two lines, but not the newline that inherently ends the buffer. After it finishes processing your command (deletion of the newline), it writes out the now joined line1line2 which is terminated by a newline, then reads line3 into the empty buffer and executes your set of commands again.

For sed to delete ALL newlines, I create a loop that keeps pulling all lines into the buffer. Here are two slightly different versions of that:

sed -e :a -e '$!N;s/\n//;ta' myfile

sed -e :a -e '$b;N;s/\n//;ba' myfile


0
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Remove all \n in sed

delete text in sed www.computing.net/answers/unix/delete-text-in-sed/8210.html

Delete all files in sub-dir +10 old www.computing.net/answers/unix/delete-all-files-in-subdir-10-old/7166.html

parameters in sed www.computing.net/answers/unix/parameters-in-sed/6942.html