The people on this forum have been a tremendous help! Thank you so much!

Here is my string


I'm trying to remove the \n and the \ from the above string.

I've tried:

tr ‘\n\r\\’ ‘   ‘

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

And a few other variations, but I'm not successful in stripping everything. I can get the \ but not the n.

Does anyone see my error?



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

You want to remove a literal \ followed by a literal n. Consequently, the backslash has to be escaped in the first substitution. By contrast, s/\n//g would remove newline characters.


Use this with tr to delete (-d) all \n and \:

tr -d '\\n\\' 


echo 'foo\nbar\foo' | tr -d '\\n\\'



You could do simply like this. You don't need to have another substitution.

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


sed 's~\\n\|\\~~g'


$ var='UEsDBBQAAAAIAKJBWEUdUIwScwwBAAB+AQAgAAAAQ29weV9vZl9kb2N1bWVudF9PY3QtMjQtMjAx\nNC5leGXsvXlcU0f0N5xAgICBRAVFRaVK3XfAigFFJYALGkSCKItWRMQNMVHrCl6ihGvc6lq1laKt\na11q3ZcgyKKouG+o2KJeDCoqCgpyn3Pm3ovY5X1\/zz\/'
$ sed 's/\\n\|\\//g' <<<"$var"


Syntax of sed:

sed 's/pattern/replace/modifier' file


Splitted the above sed command like below for clarification.

sed 's/\\n\|\\//g'
  • s - search and substitute.
  • \\n\|\\ - Pattern . \\n match a literal \n OR \| match a literal \. So at first it matches all the \n in the input string and then matches \
  • // We have an empty replacement part, which means remove all the matched characters or replace the matched characters with an empty string.
  • g - Global modifier. It makes the match to happen globally. ie, if two or more \ symbols present on a single line, this sed 's/\\//' file removes only the first \ for each line. But the global modifier g forces sed to match all the existing \ symbols.

Through GNU sed,

$ sed -r 's/\\n|\\//g' <<<"$var"

Through Perl,

$ perl -pe 's/\\n?//g' <<<"$var"
  • +1 but there's nothing GNU specific in your second sed. I mean, there is, the -r flag but that's not needed, you are using the exact saame command as your portable version and there are no features of extended regexes used. – terdon Oct 25 '14 at 12:56
  • only difference is | regex or pattern. i don't know what the title for the second one. – Avinash Raj Oct 25 '14 at 12:57
  • Ah! The escape, OK, sorry, I hadn't seen it. – terdon Oct 25 '14 at 12:59

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.