Replace an XML attribute's value with the value of a shell variable
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
<?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.
sed variable xml
add a comment |Â
up vote
0
down vote
favorite
<?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.
sed variable xml
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
<?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.
sed variable 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">
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.
sed variable xml
edited Jun 27 at 1:38
Jeff Schaller
30.8k846104
30.8k846104
asked Jun 26 at 7:22
user_297020
156
156
add a comment |Â
add a comment |Â
4 Answers
4
active
oldest
votes
up vote
1
down vote
accepted
Try this,
sed -i 's/<FOLDER NAME="[A-Z]*"/<FOLDER NAME="'$FLDR'"/g' Gather.XM
add a comment |Â
up vote
2
down vote
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
.
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
add a comment |Â
up vote
0
down vote
You are missing a ".
" in your sed expression to match all characters
sed -i "s/<FOLDER NAME=".*"/<FOLDER NAME="$FLDR"/g" Gather.XML
add a comment |Â
up vote
0
down vote
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.
add a comment |Â
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
Try this,
sed -i 's/<FOLDER NAME="[A-Z]*"/<FOLDER NAME="'$FLDR'"/g' Gather.XM
add a comment |Â
up vote
1
down vote
accepted
Try this,
sed -i 's/<FOLDER NAME="[A-Z]*"/<FOLDER NAME="'$FLDR'"/g' Gather.XM
add a comment |Â
up vote
1
down vote
accepted
up vote
1
down vote
accepted
Try this,
sed -i 's/<FOLDER NAME="[A-Z]*"/<FOLDER NAME="'$FLDR'"/g' Gather.XM
Try this,
sed -i 's/<FOLDER NAME="[A-Z]*"/<FOLDER NAME="'$FLDR'"/g' Gather.XM
answered Jun 26 at 7:58
SivaPrasath
3,88611737
3,88611737
add a comment |Â
add a comment |Â
up vote
2
down vote
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
.
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
add a comment |Â
up vote
2
down vote
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
.
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
add a comment |Â
up vote
2
down vote
up vote
2
down vote
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
.
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
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
.
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
edited Jun 26 at 11:28
answered Jun 26 at 10:10
Kusalananda
101k13199312
101k13199312
add a comment |Â
add a comment |Â
up vote
0
down vote
You are missing a ".
" in your sed expression to match all characters
sed -i "s/<FOLDER NAME=".*"/<FOLDER NAME="$FLDR"/g" Gather.XML
add a comment |Â
up vote
0
down vote
You are missing a ".
" in your sed expression to match all characters
sed -i "s/<FOLDER NAME=".*"/<FOLDER NAME="$FLDR"/g" Gather.XML
add a comment |Â
up vote
0
down vote
up vote
0
down vote
You are missing a ".
" in your sed expression to match all characters
sed -i "s/<FOLDER NAME=".*"/<FOLDER NAME="$FLDR"/g" Gather.XML
You are missing a ".
" in your sed expression to match all characters
sed -i "s/<FOLDER NAME=".*"/<FOLDER NAME="$FLDR"/g" Gather.XML
answered Jun 26 at 7:26
Arushix
9968
9968
add a comment |Â
add a comment |Â
up vote
0
down vote
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.
add a comment |Â
up vote
0
down vote
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.
add a comment |Â
up vote
0
down vote
up vote
0
down vote
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.
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.
answered Jun 26 at 7:27
Olorin
1,15711
1,15711
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f451936%2freplace-an-xml-attributes-value-with-the-value-of-a-shell-variable%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password