2011年3月31日 星期四

SED: How to remove newline (\n)?

SED: How can I replace a newline (\n)?
http://stackoverflow.com/questions/1251999/sed-how-can-i-replace-a-newline-n

sed ':a;N;$!ba;s/\n/ /g'

This will read the whole file in a loop, then replaces the newline(s) with a space.

1. create a register via :a
2. append the current and next line to the register via N
3. if we are before the last line, branch to the created register $!ba (`$! means not to do it on the last line (as there should be one final newline)).
4. finally the substitution replaces every newline with a space on the pattern space (which is the contents of the a register = the whole file.

沒有留言: