How to replace \n from a line using sed command?
Asked
Active
Viewed 5,190 times
1 Answers
9
It's gross, because sed normally processes a line at a time:
sed -e :a -e N -e 's/\n/ /' -e ta input.txt
This is nicer:
tr '\n' ' ' < input.txt
I chose to replace the newline with a space. tr can only replace by a single character (or delete with the -d option).
Flexible and simple:
perl -ne 'chomp;print $_," "' input.txt
Where " " is whatever you want in place of the newline.

Jonathan Graehl
- 9,182
- 36
- 40
-
`tr` only works with one-character strings though, which the OP didn't specify. – Flimm Jan 06 '22 at 13:52