RegEx to replace password in Linux shell script?

I have a string that contains parameters like password etc. Anything after -P is a password followed by some other parameters like -H host etc. Parameters may be in any order

Ex1: sh script.ksh -U test_user -P somepwd -H somehost

Ex2: sh script.ksh -U test_user -H somehost -P somepwd

Now, I want to replace password with the word removed

Result1: sh script.ksh -U test_user -P REMOVED -H somehost

Result2: sh script.ksh -U test_user -H somehost -P REMOVED

How, is the possible. I tried the following.

sed -i -E "s/(.*[-]P)(\b.*\b)(.*)/\1\something\3/" test_pwd_replace.txt

But, it is not working. Please help.

Upvotes: 2

Views: 185

Answers (1)

anubhava
anubhava

Reputation: 785406

You may use this sed:

sed -i -E 's/-P[[:blank:]]*[^[:blank:]]+/-P REMOVED/' test_pwd_replace.txt

[[:blank:]] is equivalent of [ \t] that matches a whitespace or a tab character.

Upvotes: 1

Related Questions