简体   繁体   中英

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

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?

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.

Change sed 's/\\\\n/\\\\\\n/g' to:

sed 's/\\n/\\\\n/g'

If you want to replace \\n with \\\\n

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM