I have some text with a password which may contain special characters (like /, *, ., [], () and other that may be used in regular expressions). How to remove that password from the text using Korn shell or maybe sed or awk? I need an answer which would be compatible with Linux and IBM AIX.
For example, the password is "123*(/abc" (it is contained in a variable). The text (it is also contained in a variable) may look like below: "connect user/123*(/abc@connection_string"
As a result I want to obtain following text: "connect user/@connection_string"
I tried to use tr -d
but received wrong result:
l_pwd='1234/\@1234'
l_txt='connect target user/1234/\@1234@connection'
print $l_txt | tr -d $l_pwd
connect target user\connection
tr -d
removes all characters in l_pwd
from l_txt
that's why the result is so strange.
Try this:
l_pwd="1234/\@1234";
escaped_pwd=$(printf '%s\n' "$l_pwd" | sed -e 's/[]\/$*.^[]/\\&/g')
l_txt="connect target user/1234/\@1234@connection";
echo $l_txt | sed "s/$escaped_pwd//g";
It prints connect target user/@connection
in bash at least.
Big caveat is this does not work on newlines, and maybe more, check out where I got this solution from.