1
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE POWERMART SYSTEM "powrmart.dtd">
<POWERMART CREATION_DATE="12/01/2016 17:43:15" REPOSITORY_VERSION="184.93">
<REPOSITORY NAME="PCREPO_BIDEV" VERSION="184" CODEPAGE="UTF-8" 
DATABASETYPE="Oracle">
<FOLDER NAME="ABC" GROUP="" OWNER="Administrator" SHARED="SHARED" 
DESCRIPTION="" PERMISSIONS="rwx---r--" UUID="3b13d2c9-39dc-426f-8320- 
def2bb8424ef">

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/<FOLDER NAME=\"\*\"/<FOLDER NAME=\"$FLDR\"/g" Gather.XML

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

0

Try this,

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

Assuming this is a well-formed XML document, using XMLStarlet:

xmlstarlet ed -u '/POWERMART/REPOSITORY/FOLDER[@NAME="ABC"]/@NAME' -v "$FLDR" file.xml

This would locate the FOLDER node under /POWERMART/REPOSITORY whose NAME attribute is ABC and change its value to the value of the shell variable FLDR.

This would also work if there happened to be a newline between the FOLDER node name and the NAME attribute, as XML is insensitive to such whitespace.

Example:

$ cat file.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE POWERMART SYSTEM "powrmart.dtd">
<POWERMART CREATION_DATE="12/01/2016 17:43:15" REPOSITORY_VERSION="184.93">
  <REPOSITORY NAME="PCREPO_BIDEV" VERSION="184" CODEPAGE="UTF-8" DATABASETYPE="Oracle">
    <FOLDER NAME="ABC" GROUP="" OWNER="Administrator" SHARED="SHARED" DESCRIPTION="" PERMISSIONS="rwx---r--" UUID="3b13d2c9-39dc-426f-8320-def2bb8424ef"/>
  </REPOSITORY>
</POWERMART>

$ FLDR='DEF'
$ xmlstarlet ed -u '/POWERMART/REPOSITORY/FOLDER[@NAME="ABC"]/@NAME' -v "$FLDR" file.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE POWERMART SYSTEM "powrmart.dtd">
<POWERMART CREATION_DATE="12/01/2016 17:43:15" REPOSITORY_VERSION="184.93">
  <REPOSITORY NAME="PCREPO_BIDEV" VERSION="184" CODEPAGE="UTF-8" DATABASETYPE="Oracle">
    <FOLDER NAME="DEF" GROUP="" OWNER="Administrator" SHARED="SHARED" DESCRIPTION="" PERMISSIONS="rwx---r--" UUID="3b13d2c9-39dc-426f-8320-def2bb8424ef"/>
  </REPOSITORY>
</POWERMART>

If you have to match a particular REPOSITORY's NAME, then e.g.

xmlstarlet ed -u '/POWERMART/REPOSITORY[@NAME="PCREPO_BIDEV"]/FOLDER[@NAME="ABC"]/@NAME' -v "$FLDR" file.xml
| improve this answer | |
0

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

 sed -i "s/<FOLDER NAME=\"\.*\"/<FOLDER NAME=\"$FLDR\"/g" Gather.XML
| improve this answer | |
0

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.

| improve this answer | |

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.