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
- append the current and next line to the pattern space via
- if we are before the last line, branch to the created label
$!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 -z 's/n/,/g'
-z is included since 4.2.2
-z changes the delimiter to null characters (