Sunday, May 20, 2012

Linux / UNIX: Sed Replace Newline

How do I replace newline (\n) with sed under UNIX / Linux operating systems?

You can use the following sed command:
sed '{:q;N;s/\n//g;t q}' /path/to/data.txt
You can replace newline (\n) with * character or word 'FOO':
sed '{:q;N;s/\n/*/g;t q}' /path/to/data.txt
OR
sed '{:q;N;s/\n/FOO/g;t q}' /path/to/data.txt
OR replace it with tab (\t):
sed '{:q;N;s/\n/\t/g;t q}' /path/to/data.txt
To update file use -i option:
sed -i '{:q;N;s/\n/\t/g;t q}' /path/to/data.txt

No comments:

Post a Comment