I have to replace special characters using shell, so i use sed but i have some mistakes that i don't understand.

<%_ by [@, ("_" = dash)
_%> by ]

for the first 2 characters my synthax is : sed -i y/\<%\/\]\/ test.htm it works, but here how can i add the dash character ? The second should be this way sed -i y/\%>\/\]\/ but i have this mistake bash: /]/: is a folder can you help me please

  • _ is underscore, - is dash. Which of these do you want to replace? – Barmar Apr 17 '15 at 21:22
  • You need to quote the sed argument that contains the subsitution, because it includes special shell characters. – Barmar Apr 17 '15 at 21:23
  • And the command to do string substitution is s/from/to/. y is for doing character-by-character translation. – Barmar Apr 17 '15 at 21:24

I'm not sure what you are trying to replace (with a - or _ in the first case) but this works fine for me:

$ echo "<%_" | sed 's/<%_/_/'
$ echo "<%_" | sed 's/<%_/-/'
$ echo "_%>" | sed 's/_%>/\]/'

It looks like you are escaping the regex identifiers (the /).

  • Apparently the only character that needs to be escaped for me is the ], everything else works fine (not special in the regex provided) – Micah Manary Apr 17 '15 at 17:07

You need to quote the argument, and use the s command to perform string substitution. You can use the -e option to supply multiple command expressions.

sed -i -e 's/<% /[@/' -e 's/ %>/]/' filename
  • thank for your answer, but i have a mistake with ']', it's output bash: /]/: is a folder so i don't understand why. Also you can consider the dash as a space, i did not know how to explain it. In my file it is like this <% and ` %>` – iceman225 Apr 20 '15 at 8:34
  • You should only get an error like that if you forgot to quote the arguments. An unquoted > is used to redirect output of a command. – Barmar Apr 20 '15 at 23:28
  • finally sed 's/%>/]/g' file – iceman225 Apr 21 '15 at 9:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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