Mon 9 Jun 2008
If you are like me, at some point you have tried to remove newlines from a file with SED.
I have encountered this many times, the nuance always seems to escape me the next time I need it.
The reason it can’t be done in the manner you are most likely trying (sed ’s/\n//g’ infile > outfile), is because SED operates on one line at a time, and generally ignores the newline character.
An easy way to accomplish this is to use awk, or tr:
awk '{ORS=" "; print $0}' infile > outfile
or
cat infile|tr '\n' ' '
If you are stubborn, and still want to use sed;
sed -n ':a;N;$!ba;s/\n//g;p' infile >outfile