0

I have encrypted passwords specific to each environment. It has some special characters. Below is one sample from development environment (which has forward slash)

    $$Param_INFA_USER=USR_DF
    $$Param_INFA_PASS=1OFR6pSbNq/yvLtpxHbC9E9KvloTj5tRGpzr9dCMD7E=
v_prst_pwd=`grep -i "\\$\\$Param_INFA_PASS" param_file.parm` | tr -d ' '
v_lbl_pwd=`grep -i "\\$\\$Param_INFA_PASS" param_file.parm | cut -d '=' -f1`
sed -i 's/'$v_prst_pwd'/'$v_lbl_pwd'='$envspfc_pwd'/g' param_file.parm
sed -i s/$$Param_INFA_PASS=**1OFR6pSbNq/yvLtpxHbC9E9KvloTj5tRGpzr9dCMD7E=**/$$Param_INFA_PASS=ABC/g param_file.parm

I am trying to replace passwords and got below issue.

sed: -e expression #1, char 74: unknown option to `s'

How to escape special characters when replacing a string which is a variable (not aware of position of special characters).

Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232

2 Answers2

1

The value of Param_INFA_PASS is evidently Base64. It can contain letters, digits and the characters +, / and =.

For plain grep, none of these characters are special. Beware that for egrep or grep -E, the character + is special and would need to be escaped. Don't use grep -i: Base64 is case-sensitive.

For sed, none of these characters are special if you do it right. In the s command, you can use any character¹ instead of /. So pick one that isn't used in Base64, for example

sed -e "s~^PASSWORD=$old_password\$~$new_password~g"

¹ Except newline and backslash.

0

sed does not use / as its separator.

Because of the examples, we think that sed uses / as a separator. However it can use any character.

e.g. sed 's/abc/def/' can be written as sed 's^abc^def^. What ever follows the s is the separator.

If there is a character that is not used in the input text then you can use this to your advantage (Maybe choose a non-printing character).