Home > Net >  Replace XML attributes value with random number in shell script
Replace XML attributes value with random number in shell script

Time:09-17

Need to replace the RC value with random number in shell script my test.xml file contain below XML snippet

<doc:meta title = 'test-title'></doc:meta><doc:meta title = 'test-title-2'></doc:meta>

Expected Ouput

<doc:meta title = 'test-title'></doc:meta><doc:meta title = 'test-title-2'></doc:meta>

Actual Results: <doc:meta title = 'test-title'></doc:meta>

I have tried this but it is not working

var2=$RANDOM 
sed -i "s/^<item name="RC" .*/<item value=\"$var2\">/g;" test.xml

CodePudding user response:

You can use extended regex :

var2=$RANDOM 
sed -E -i 's|^(<item name="RC" value=").*("></item>)|\1'$var2'\2|' test.xml

CodePudding user response:

You need to escape the embedded quotes:

sed "/<item name=\"RC\"/s/value=[^>] /name=\"$var2\">/"
  • Related