Is there an issue with sed and new line character?
I have a file test.txt with the following contents
aaaaa
bbbbb
ccccc
ddddd
The following does not work:
sed -r -i 's/\n/,/g' test.txt
I know that I can use tr
for this but my question is why it seems not possible with sed.
If this is a side effect of processing the file line by line I would be interested in why this happens. I think grep
removes new lines. Does sed do the same?
Best Answer
With GNU
sed
and providedPOSIXLY_CORRECT
is not in the environment (for single-line input):From https://stackoverflow.com/questions/1251999/sed-how-can-i-replace-a-newline-n :
:a
N
$!ba
($!
means not to do it on the last line (as there should be one final newline)).