Linux - Replace newline using sed, Sed Command replace newline

Replace newline using sed

How to replace newline using sed command in linux or unix

 # sed '{:q;N;s/\n//g;t q}' /change/newline.txt

 # sed ':a;N;$!ba;s/\n/ /g'
Explanation of the command how it is used:
1) Create a label via :a
2) Append the current and next line to the pattern space via N
3) If we are before the last line, branch to the created label $!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 whole file).


You can replace newline (\n) with * character or word 'FOO':
 sed '{:q;N;s/\n/*/g;t q}' /change/newline.txt

 sed '{:q;N;s/\n/FOO/g;t q}' /change/newline.txt

To replace newline(\n) with tab (\t):
 # sed '{:q;N;s/\n/\t/g;t q}' /change/newline.txt
To update file use -i option:
 # sed -i '{:q;N;s/\n/\t/g;t q}' /change/newline.txt

tr commnad to replace , with \n
 # tr , '\n' < file

The topic on Linux - Replace newline using sed is posted by - Guru

Hope you have enjoyed, Linux - Replace newline using sedThanks for your time

Tech Bluff