Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

Thanks!

share|improve this question
    
Thanks to everyone :) – forestclown Mar 16 '12 at 1:57
up vote 1 down vote accepted

You should slightly change your sed command:

sed 's/\("Password":\"\).*\(\",\"Name\":\)/\1XXX\2/'
share|improve this answer

Below regex is the base one, place necessary escape characters

(\s"Password":")(.*?)(","Name":"(.*?)")

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/'
share|improve this answer

This might work for you:

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .