nwalke
nwalke

Reputation: 3209

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?

Upvotes: 3

Views: 6956

Answers (2)

Jahid
Jahid

Reputation: 22438

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

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

If you want to replace \n with \\n

Upvotes: 2

Rakholiya Jenish
Rakholiya Jenish

Reputation: 3223

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.

Upvotes: 2

Related Questions