Answer by Walter A for Replace newlines with literal \n
With the -zoption you can dosed -z 's/\n/\\n/g' fileorsed -z "s/\n/\\\n/g" file
View ArticleAnswer by RSX for Replace newlines with literal \n
In case it helps anyone, I was searching for the opposite of this question: to replace literal '\'n in a string with newline. I managed to solve it with sed like this:_s="foo\nbar\n"echo $_s | sed...
View ArticleAnswer by mvr for Replace newlines with literal \n
You could do this using sed and tr:sed 's/$/\\n/' file | tr -d '\n'However this will add an extra \n at the end.
View ArticleAnswer by Ed Morton for Replace newlines with literal \n
Is this what you're trying to do?$ cat fileabc$ awk '{printf "%s\\n", $0}' filea\nb\nc\n$or even:$ awk -v ORS='\\n''1' filea\nb\nc\n$Run dos2unix on the input file first to strip the \rs if you like,...
View ArticleAnswer by anubhava for Replace newlines with literal \n
This should work with both LF or CR-LF line endings:sed -E ':a;N;$!ba;s/\r{0,1}\n/\\n/g' file
View ArticleReplace newlines with literal \n
This stackoverflow question has an answer to replace newlines with sed, using the format sed ':a;N;$!ba;s/\n/ /g'.This works, but not for special characters like \r, \n, etc.What I'm trying to do is to...
View ArticleAnswer by Sardorbek Imomaliev for Replace newlines with literal \n
On macOS, because is ; command delimiter is not supported you could use newline delimiter instead (extended explanation https://stackoverflow.com/a/79080850/3627387)$ sed ':aN$!bas/\n/\\n/g' file
View Article