Can sed replace new line characters?

With GNU sed and provided POSIXLY_CORRECT is not in the environment (for single-line input):

sed -i ':a;N;$!ba;s/\n/,/g' test.txt

From https://stackoverflow.com/questions/1251999/sed-how-can-i-replace-a-newline-n :

  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 comma on the pattern space (which is the whole file).

This works with GNU sed:

sed -z 's/\n/,/g' 

-z is included since 4.2.2

NB. -z changes the delimiter to null characters (\0). If your input does not contain any null characters, the whole input is treated as a single line. This can come with its limitations.

To avoid having the newline of the last line replaced, you can change it back:

sed -z 's/\n/,/g;s/,$/\n/'

(Which is GNU sed syntax again, but it doesn't matter as the whole thing is GNU only)


sed always removes the trailing \newline just before populating pattern space, and then appends one before writing out the results of its script. A \newline can be had in pattern-space by various means - but never if it is not the result of an edit. This is important - \newlines in sed's pattern space always reflect a change, and never occur in the input stream. \newlines are the only delimiter a sedder can count on with unknown input.

If you want to replace all \newlines with commas and your file is not very large, then you can do:

sed 'H;1h;$!d;x;y/\n/,/'

That appends every input line to hold space - except the first, which instead overwrites hold space - following a \newline character. It then deletes every line not the $!last from output. On the last line Hold and pattern spaces are exchanged and all \newline characters are y///translated to commas.

For large files this sort of thing is bound to cause problems - sed's buffer on line-boundaries, that can be easily overflowed with actions of this sort.

Tags:

Sed