How can I replace each newline (\n) with a space using sed?

sed is intended to be used on line-based input. Although it can do what you need.

A better option here is to use the tr command as follows:

tr '\n' ' ' < input_filename

or remove the newline characters entirely:

tr -d '\n' < input.txt > output.txt

or if you have the GNU version (with its long options)

tr --delete '\n' < input.txt > output.txt

Leave a Comment