1

I am trying to replace string within quotes in a file! Here is an example :

<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Manager pathname="" />
<Resource auth="Container" driverClassName="org.postgresql.Driver"   maxActive="20" maxIdle="5" maxWait="10000" name="jdbc/custom" password="CHANGEIT" type="javax.sql.DataSource" url="jdbc:postgresql://10.10.10.10:5432/database" username="MyUser"/>
</Context>

This command works for several 'fields' but it doesn't work with username because of / .

# It's OK
sed 's/\(.*password=\)[^ ]*\( .*\)/\1"NEW_VALUE"\2/' ROOT.xml

# It's NOK
sed 's/\(.*username=\)[^ ]*\( .*\)/\1"NEW_VALUE"\2/' ROOT.xml

How can I do that please ?

Thank you,

0

You have to change the command

sed 's/\(.*username=\)[^ ]*\( .*\)/\1"NEW_VALUE"\2/' ROOT.xml

into

sed 's/\(.*username=\)[^ \/]*\( .*\)/\1"NEW_VALUE"\2/' ROOT.xml

because [^ ] means: Every character except a blank. [^ \/] means: Every character except a blank and/or /.

  • Thanks... but it doesn't work for me :( – Djé Djé Feb 15 '17 at 9:50
  • What do you get, if you run my adjusted command on the text in your initial question? – kristian Feb 15 '17 at 10:06
  • I've got this : [root@mymachine ~]# sed 's/(.*username=)[^ \/]*( .*)/\1"NEW_VALUE"\2/' ROOT.xml <?xml version="1.0" encoding="UTF-8"?> <Context> <Manager pathname="" /> <Resource auth="Container" driverClassName="org.postgresql.Driver" maxActive="20" maxIdle="5" maxWait="10000" name="jdbc/custom" password="CHANGEIT" type="javax.sql.DataSource" url="jdbc:postgresql://10.10.10.10:5432/database" username="MyUser"/> </Context> No changes made – Djé Djé Feb 15 '17 at 11:51
  • Why are you calling sed with (.*username=) instead of \(.*username=\) and ( .*) instead of \( .*\) ? Look here: grymoire.com/Unix/Regular.html#uh-10 – kristian Feb 15 '17 at 12:20
  • Look at your command: sed 's/(.*username=)[^ \/]*( .*)/\1"NEW_VALUE"\2/' ROOT.xml. Then look at my comment. – kristian Feb 15 '17 at 13:16

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.