Using sed to make text that contains certain characters suitable for use in string literals is straightforward:
sed "s/\\\\/\\\\\\\\/g"
sed "s/\\\"/\\\\\\\"/g"But how can I do something similar with a text file containing newline characters?
17 Answers
If you're dealing with a pipelined command, then I like @vossad01's answer. However, in a multi-line script, I prefer normal Bash parameter expansion, which is both more efficient (doesn't need to fork a new process for sed or create any pipes) and perhaps a bit easier to read:
${varName//$'\n'/\\n}Reader's guide:
${...}- Interpret the internal stuff using parameter expansionvarName- Name of the variable containing the content//- Replace all instances of...$'\n'- The literal newline character (we're turning the escape into the literal here)/- Replace with...\\n- The newline escape sequence ("\n"): note that we had to escape the backslash itself
Example:
$ foo="$(printf '%s\n' $(seq 1 3))"
$ echo "$foo"
1
2
3
$ echo "${foo//$'\n'/\\n}"
1\n2\n3 Use:
sed ':a;N;$!ba;s/\n/\\n/g'Which uses the answer from How can I replace a newline (\n) using sed? substituting an escaped newline character as the replacement to match the question here.
1I solved this one with:
sed 's/$/\\n/g' | tr -d'\n'sed puts '\\n' at the end of every line, then tr removes the newlines.
1sed is line based, and this can cause issues when trying to replace newline characters.
the official documentation for sed makes a specific reference to newline characters and states they are stripped off before being passed to sed.
I would suggest that 'tr' would probably be a better fit here.
as an example, to replace newline characters with spaces:
tr '\n' ' ' < inputfile 6 I'd like to extend David Moytan's solution:
cat /etc/passwd | perl -e 'while(<>) { $_ =~ s/[\r\n]/__NEWLINE__/g; print "$_" }' I had this problem too.
You can use the \a function to start every newline with the proper escape sequence, then pipe the resulting function into echo -n which suppresses newlines.
echo -n $(sed '\a
\\n');Stick whatever you're sed-ing in the parentheses before the sed command.
This awk solution worked for me:
$ awk 1 ORS='\\n' file.txtCredit goes to this excellent answer.