1

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!

0

3 Answers 3

2

You should slightly change your sed command:

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

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/'
1

This might work for you:

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

You must log in to answer this question.

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