Replacing newline escape '\n' with an escaped newline escape '\\n' using sed

送分小仙女□ 提交于 2020-05-15 06:50:46

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!