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

Remove all \n in sed

Reply to Message Icon

Original Message
Name: nmf
Date: May 6, 2006 at 16:34:38 Pacific
Subject: Remove all \n in sed
OS: mac
CPU/Ram: emac
Model/Manufacturer: 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.


Report Offensive Message For Removal


Response Number 1
Name: James Boothe
Date: June 8, 2006 at 12:07:30 Pacific
Subject: Remove all \n in sed
Reply: (edit)

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


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Remove all \n in sed

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




How often do you use Computing.Net?

Every Day
Once a Week
Once a Month
This Is My First Time!


View Results

Poll Finishes In 4 Days.
Discuss in The Lounge