Remove substring which may contain special characters in Korn shell scripting

Viewed 35

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
2 Answers

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.

With ksh 93u+

Here's some parameter expansion madness:

$ echo "${l_txt//${l_pwd//[\/\\]/?}/}"
connect target user/@connection

That takes the password variable, substitutes forward and back slashes into ?, then uses that as a pattern to remove from the connection string.


A more robust version:

x=${l_pwd//[^[:alnum:]]/\\\0}
typeset -p x                        # => x='1234\/\\\@1234'

conn=${l_txt%%${x}*}${l_txt#*${x}}
typeset -p conn                     # => connect target user/@connection
Related