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 :
- create a label via
:a
- append the current and next line to the pattern space via
N
- 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)). - 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 \n
ewline just before populating pattern space, and then appends one before writing out the results of its script. A \n
ewline can be had in pattern-space by various means - but never if it is not the result of an edit. This is important - \n
ewlines in sed
's pattern space always reflect a change, and never occur in the input stream. \n
ewlines are the only delimiter a sed
der can count on with unknown input.
If you want to replace all \n
ewlines 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 h
old space - except the first, which instead overwrites h
old space - following a \n
ewline character. It then d
eletes every line not the $!
last from output. On the last line H
old and pattern spaces are ex
changed and all \n
ewline 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.