I got a text file with contents like below

2012/03/15 : "Password":"@#4JF9u92r","Name":"John"

I wish to use sed to change the above to

2012/03/15 : "Password":"XXX","Name":"John"

I have tried the following command

cat log.txt | sed 's/\("Password":\"\)\(.*\)\(\",\"Name\":\)/\1XXX/'

But it gives me

2012/03/15 : "Password":"XXX"John"

What did I do wrong?



You should slightly change your sed command:

sed 's/\("Password":\"\).*\(\",\"Name\":\)/\1XXX\2/'

Below regex is the base one, place necessary escape characters


Replace with \1XXX\3

After placing proper escape characters, I think it should be something like this.

cat log.txt | sed '\(s/\"Password\":\"\)\(.*?\)\(\",\"Name\":\"\(.*?\)\"\)/\1XXX\3/'

This might work for you:

echo '2012/03/15 : "Password":"@#4JF9u92r","Name":"John"' |
sed 's/\("Password":"\)[^"]*/\1XXX/'
2012/03/15 : "Password":"XXX","Name":"John"
  • Thanks, this is more concise...but might not work if password contain the " character.. – forestclown Mar 16 '12 at 1:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.