Need help with sed to escape special characters

Hello Everyone,

I need to read an encrypted password from the user and update that value in an xml file. I am trying to use "sed" for searching the appropriate tag and replacing this new value that get from the user. Since the encrypted password can contain special characters(like /,\,&,etc), the "sed" command is coming garbled. Could somebody please help to find an efficient solution for this problem?

Is there a way to instruct sed command not to consider any special characters in the replacement string?

Something like

s/${key}/"what ever string"/

Or

Any efficient way to put escape sequence for the special characters?

I tried something like

's/\|/\\|/g' , 's|\\|\\\\|g'

to escape individual special characters. But this is not working for special characters like &,/ etc.... Is it possible to write any generic pattern which can handle any special characters.

Very much appreciate any help

Thanks,
Martin

Delimiter in sed can be anything, not just slash:

sed 's!what!that!'

Hello Mirni,

Thanks for the response. I have problem here as I do not know the search string in advance, it's being read from the user. So the user can enter any character in the input, which may contain the same delimiter character that I used..

Thanks,
Martin

use : as delimiter. Password will not contain the the :
sed 's:searchstring:replacestring:g'

Thanks,
Kalai

What exactly are you trying to do with the sed command?
If you are storing the passwords, you should consider hashing them and store the hashes instead (in which case you cannot retrieve them, you'll need to reset them).