问题
I am trying to replace the literal term \n
(not a newline, the literal) by the literal \\n
using sed
. I've tried this:
echo '"Refreshing \n\n\n state prior"' | sed 's/\\n/\\\n/g'
This "works" but I need it to output the literal characters \\n
. Right now I end up with something like this:
"Refreshing \
\
\
state prior"
Is there a way for me to maintain the \\n
in sed
output?
回答1:
To get \\n
add one more \
to your sed:
echo "Refreshing \n\n\n state prior" | sed 's/\\n/\\\\n/g'
What you were trying to do with \\\n
was to print \
character and then add \n
which caused a new line.
回答2:
Change sed 's/\\n/\\\n/g'
to:
sed 's/\\n/\\\\n/g'
If you want to replace \n
with \\n
来源:https://stackoverflow.com/questions/30785650/replacing-newline-escape-n-with-an-escaped-newline-escape-n-using-sed