Skip to main content

Replace an XML attribute's value with the value of a shell variable [Resolved]

I have the above sample data. In which file will have FOLDER NAME with some value. I have to replace it with some other value which is a variable I want to replace FOLDER NAME="ABC" with DEF

 sed -i "s/

Above sed commands doesn't through any error but it is not replacing.

Question Credit: user_297020
Question Reference
Asked March 17, 2019
Tags: , variable
Posted Under: Unix Linux
4 Answers

Try this,

sed -i 's/<FOLDER NAME="[A-Z]*"/<FOLDER NAME="'$FLDR'"/g' Gather.XM

credit: msp9011
Answered March 17, 2019

You are missing a "." in your sed expression to match all characters

 sed -i "s/<FOLDER NAME=\"\.*\"/<FOLDER NAME=\"$FLDR\"/g" Gather.XML

credit: Arushix
Answered March 17, 2019

It looks like you should do [^\"]* instead of \*:

sed -i "s/<FOLDER NAME=\"[\"]*\"/<FOLDER NAME=\"$FLDR\"/g" Gather.XML

Since you want to match all text till the first " there. \* will match the literal * character, but you don't have that there, so the file's contents never match.

credit: Olorin
Answered March 17, 2019
Your Answer