0

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

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

2 Answers 2

1

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 /).

1
  • Apparently the only character that needs to be escaped for me is the ], everything else works fine (not special in the regex provided) Commented Apr 17, 2015 at 17:07
1

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
3
  • 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
    Commented Apr 20, 2015 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
    Commented Apr 20, 2015 at 23:28
  • finally sed 's/%>/]/g' file
    – iceman225
    Commented Apr 21, 2015 at 9:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .