Search replace in XML file with sed or awk
Clash Royale CLAN TAG#URR8PPP
up vote
3
down vote
favorite
So I have a task where by I have to manipulate an XML file through a bash shell script.
Here are the steps:
- Query XML file for a value.
- Take the value and cross reference it to find a new value from a list.
- Replace the value of a different element with the new value.
Here is a sample of the XML with non-essential info removed:
<fmreq:fileManagementRequestDetail xmlns:fmreq="http://foobar.com/filemanagement">
<fmreq:property>
<fmreq:name>form_category_cd</fmreq:name>
<fmreq:value>Memos</fmreq:value>
</fmreq:property>
<fmreq:property>
<fmreq:name>object_name</fmreq:name>
<fmreq:value>Correspondence</fmreq:value>
</fmreq:property>
</fmreq:fileManagementRequestDetail>
I have to get the value from the value element under object_name, cross reference it, and then replace the value under the form_category_cd value element with the new value:
So if object_name -> value is Correspondence then the form_category_cd -> value might need to be YYZ.
Here's the rub, I can only use the tools available on our server as our operations group is restricting us to the tools at hand. It was a fight to get xmllint updated and then it got overruled. I'm on a version that does not support --xpath, which believe me is difficult on a good day. Also the version I have available doesn't support namespaces, so xmllint is out.
I've tried sed, but it seems to not like my regex even though every tester I try works fine.
Regex:
(<fmreq:name>object_name</fmreq:name>)(?:ns*)(<fmreq:value>)(.*)(</fmreq:value>)
I need to get group #3, but sed won't return it. Instead it returns the entire contents of the XML file.
sed -e 's/(<fmreq:name>object_name</fmreq:name>)(?:ns*)(<fmreq:value>)(.*)(</fmreq:value>)/3/' < c3.xml
I'm not as familiar with awk / gawk, so I'm struggling to figure them out and this as well, but am open to them if a solution can be found.
Would love to have an awk / gawk solution just to make the boss happy since he's an old awk fan, but I'll take what I can get as I'm stumped.
Again I have to use the tools on hand and can't install anything new.
text-processing awk sed regular-expression xml
add a comment |Â
up vote
3
down vote
favorite
So I have a task where by I have to manipulate an XML file through a bash shell script.
Here are the steps:
- Query XML file for a value.
- Take the value and cross reference it to find a new value from a list.
- Replace the value of a different element with the new value.
Here is a sample of the XML with non-essential info removed:
<fmreq:fileManagementRequestDetail xmlns:fmreq="http://foobar.com/filemanagement">
<fmreq:property>
<fmreq:name>form_category_cd</fmreq:name>
<fmreq:value>Memos</fmreq:value>
</fmreq:property>
<fmreq:property>
<fmreq:name>object_name</fmreq:name>
<fmreq:value>Correspondence</fmreq:value>
</fmreq:property>
</fmreq:fileManagementRequestDetail>
I have to get the value from the value element under object_name, cross reference it, and then replace the value under the form_category_cd value element with the new value:
So if object_name -> value is Correspondence then the form_category_cd -> value might need to be YYZ.
Here's the rub, I can only use the tools available on our server as our operations group is restricting us to the tools at hand. It was a fight to get xmllint updated and then it got overruled. I'm on a version that does not support --xpath, which believe me is difficult on a good day. Also the version I have available doesn't support namespaces, so xmllint is out.
I've tried sed, but it seems to not like my regex even though every tester I try works fine.
Regex:
(<fmreq:name>object_name</fmreq:name>)(?:ns*)(<fmreq:value>)(.*)(</fmreq:value>)
I need to get group #3, but sed won't return it. Instead it returns the entire contents of the XML file.
sed -e 's/(<fmreq:name>object_name</fmreq:name>)(?:ns*)(<fmreq:value>)(.*)(</fmreq:value>)/3/' < c3.xml
I'm not as familiar with awk / gawk, so I'm struggling to figure them out and this as well, but am open to them if a solution can be found.
Would love to have an awk / gawk solution just to make the boss happy since he's an old awk fan, but I'll take what I can get as I'm stumped.
Again I have to use the tools on hand and can't install anything new.
text-processing awk sed regular-expression xml
1
XML processing is best done with a tool made for the job (bash and sed and awk are not made for the job).
â Jeff Schaller
Oct 14 '17 at 21:22
2
Where does "YYZ" come from?
â RobertL
Oct 14 '17 at 22:05
What's the format of the list file that holds the new values?
â seshoumara
Oct 14 '17 at 23:52
YYZ is just an example value (also the name of a Rush tune).
â Bob Lyman
Oct 15 '17 at 20:55
What's actually happening is the value from object_name is eval'd against a set of know values then the value under form_category_cd is replaced with that new value and the file written out. Yes I would love to not use sed and bash, but our operations group will not allow something like python or perl on the server because they don't have resources to support then. Believe this is a battle that was fought and lost by a co-worker.
â Bob Lyman
Oct 15 '17 at 20:58
add a comment |Â
up vote
3
down vote
favorite
up vote
3
down vote
favorite
So I have a task where by I have to manipulate an XML file through a bash shell script.
Here are the steps:
- Query XML file for a value.
- Take the value and cross reference it to find a new value from a list.
- Replace the value of a different element with the new value.
Here is a sample of the XML with non-essential info removed:
<fmreq:fileManagementRequestDetail xmlns:fmreq="http://foobar.com/filemanagement">
<fmreq:property>
<fmreq:name>form_category_cd</fmreq:name>
<fmreq:value>Memos</fmreq:value>
</fmreq:property>
<fmreq:property>
<fmreq:name>object_name</fmreq:name>
<fmreq:value>Correspondence</fmreq:value>
</fmreq:property>
</fmreq:fileManagementRequestDetail>
I have to get the value from the value element under object_name, cross reference it, and then replace the value under the form_category_cd value element with the new value:
So if object_name -> value is Correspondence then the form_category_cd -> value might need to be YYZ.
Here's the rub, I can only use the tools available on our server as our operations group is restricting us to the tools at hand. It was a fight to get xmllint updated and then it got overruled. I'm on a version that does not support --xpath, which believe me is difficult on a good day. Also the version I have available doesn't support namespaces, so xmllint is out.
I've tried sed, but it seems to not like my regex even though every tester I try works fine.
Regex:
(<fmreq:name>object_name</fmreq:name>)(?:ns*)(<fmreq:value>)(.*)(</fmreq:value>)
I need to get group #3, but sed won't return it. Instead it returns the entire contents of the XML file.
sed -e 's/(<fmreq:name>object_name</fmreq:name>)(?:ns*)(<fmreq:value>)(.*)(</fmreq:value>)/3/' < c3.xml
I'm not as familiar with awk / gawk, so I'm struggling to figure them out and this as well, but am open to them if a solution can be found.
Would love to have an awk / gawk solution just to make the boss happy since he's an old awk fan, but I'll take what I can get as I'm stumped.
Again I have to use the tools on hand and can't install anything new.
text-processing awk sed regular-expression xml
So I have a task where by I have to manipulate an XML file through a bash shell script.
Here are the steps:
- Query XML file for a value.
- Take the value and cross reference it to find a new value from a list.
- Replace the value of a different element with the new value.
Here is a sample of the XML with non-essential info removed:
<fmreq:fileManagementRequestDetail xmlns:fmreq="http://foobar.com/filemanagement">
<fmreq:property>
<fmreq:name>form_category_cd</fmreq:name>
<fmreq:value>Memos</fmreq:value>
</fmreq:property>
<fmreq:property>
<fmreq:name>object_name</fmreq:name>
<fmreq:value>Correspondence</fmreq:value>
</fmreq:property>
</fmreq:fileManagementRequestDetail>
I have to get the value from the value element under object_name, cross reference it, and then replace the value under the form_category_cd value element with the new value:
So if object_name -> value is Correspondence then the form_category_cd -> value might need to be YYZ.
Here's the rub, I can only use the tools available on our server as our operations group is restricting us to the tools at hand. It was a fight to get xmllint updated and then it got overruled. I'm on a version that does not support --xpath, which believe me is difficult on a good day. Also the version I have available doesn't support namespaces, so xmllint is out.
I've tried sed, but it seems to not like my regex even though every tester I try works fine.
Regex:
(<fmreq:name>object_name</fmreq:name>)(?:ns*)(<fmreq:value>)(.*)(</fmreq:value>)
I need to get group #3, but sed won't return it. Instead it returns the entire contents of the XML file.
sed -e 's/(<fmreq:name>object_name</fmreq:name>)(?:ns*)(<fmreq:value>)(.*)(</fmreq:value>)/3/' < c3.xml
I'm not as familiar with awk / gawk, so I'm struggling to figure them out and this as well, but am open to them if a solution can be found.
Would love to have an awk / gawk solution just to make the boss happy since he's an old awk fan, but I'll take what I can get as I'm stumped.
Again I have to use the tools on hand and can't install anything new.
text-processing awk sed regular-expression xml
edited Oct 14 '17 at 21:32
Jeff Schaller
32.1k849109
32.1k849109
asked Oct 14 '17 at 21:11
Bob Lyman
1815
1815
1
XML processing is best done with a tool made for the job (bash and sed and awk are not made for the job).
â Jeff Schaller
Oct 14 '17 at 21:22
2
Where does "YYZ" come from?
â RobertL
Oct 14 '17 at 22:05
What's the format of the list file that holds the new values?
â seshoumara
Oct 14 '17 at 23:52
YYZ is just an example value (also the name of a Rush tune).
â Bob Lyman
Oct 15 '17 at 20:55
What's actually happening is the value from object_name is eval'd against a set of know values then the value under form_category_cd is replaced with that new value and the file written out. Yes I would love to not use sed and bash, but our operations group will not allow something like python or perl on the server because they don't have resources to support then. Believe this is a battle that was fought and lost by a co-worker.
â Bob Lyman
Oct 15 '17 at 20:58
add a comment |Â
1
XML processing is best done with a tool made for the job (bash and sed and awk are not made for the job).
â Jeff Schaller
Oct 14 '17 at 21:22
2
Where does "YYZ" come from?
â RobertL
Oct 14 '17 at 22:05
What's the format of the list file that holds the new values?
â seshoumara
Oct 14 '17 at 23:52
YYZ is just an example value (also the name of a Rush tune).
â Bob Lyman
Oct 15 '17 at 20:55
What's actually happening is the value from object_name is eval'd against a set of know values then the value under form_category_cd is replaced with that new value and the file written out. Yes I would love to not use sed and bash, but our operations group will not allow something like python or perl on the server because they don't have resources to support then. Believe this is a battle that was fought and lost by a co-worker.
â Bob Lyman
Oct 15 '17 at 20:58
1
1
XML processing is best done with a tool made for the job (bash and sed and awk are not made for the job).
â Jeff Schaller
Oct 14 '17 at 21:22
XML processing is best done with a tool made for the job (bash and sed and awk are not made for the job).
â Jeff Schaller
Oct 14 '17 at 21:22
2
2
Where does "YYZ" come from?
â RobertL
Oct 14 '17 at 22:05
Where does "YYZ" come from?
â RobertL
Oct 14 '17 at 22:05
What's the format of the list file that holds the new values?
â seshoumara
Oct 14 '17 at 23:52
What's the format of the list file that holds the new values?
â seshoumara
Oct 14 '17 at 23:52
YYZ is just an example value (also the name of a Rush tune).
â Bob Lyman
Oct 15 '17 at 20:55
YYZ is just an example value (also the name of a Rush tune).
â Bob Lyman
Oct 15 '17 at 20:55
What's actually happening is the value from object_name is eval'd against a set of know values then the value under form_category_cd is replaced with that new value and the file written out. Yes I would love to not use sed and bash, but our operations group will not allow something like python or perl on the server because they don't have resources to support then. Believe this is a battle that was fought and lost by a co-worker.
â Bob Lyman
Oct 15 '17 at 20:58
What's actually happening is the value from object_name is eval'd against a set of know values then the value under form_category_cd is replaced with that new value and the file written out. Yes I would love to not use sed and bash, but our operations group will not allow something like python or perl on the server because they don't have resources to support then. Believe this is a battle that was fought and lost by a co-worker.
â Bob Lyman
Oct 15 '17 at 20:58
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
2
down vote
accepted
I think that there are a couple of problems in your sed
command:
You don't use the
-n
option, so by defaultsed
just prints every line of input to the output (possibly modified by ased
command).You don't need the redirection
< c3.xml
, becausesed
recognizes the last argument as a filename.sed
is not very well suited for matches over multiple lines. See for example here.
The following seems to work on your example:
sed -n "/<fmreq:name>object_name</fmreq:name>/ n;p" c3.xml | sed "s/^s*<fmreq:value>(.*)</fmreq:value>/1/g"
Or, with only one sed
invocation:
sed -n "/<fmreq:name>object_name</fmreq:name>/ n;s/^s*<fmreq:value>(.*)</fmreq:value>/1/g;p" c3.xml
Breakdown of what this command does:
The option
-n
tellssed
not to print the pattern space after it's finished processing the line. Consequently, you need to use the commandp
explicitely to do so./regex/
tellssed
to execute the commands that follow only on the lines that matchregex
.The
sed
commandn
replaces the content of the pattern space by the next line of input, which is the one containing the value you are interested in.The
sed
commands/regex/replacement/
substitutes the first match ofregex
in the pattern space byreplacement
.The
sed
commandp
prints the line.
Thanks, I'm still getting used to using sed so any obvious errors are mine and I appreciate any help given.
â Bob Lyman
Oct 15 '17 at 20:59
The redirection is a leftover from the original command as the operation has to eventually replace another value with a redirect to another file.
â Bob Lyman
Oct 15 '17 at 21:31
Glad it helped. But yes,sed
and regular expressions do take some time to get used to. :) All the best!
â Rastapopoulos
Oct 16 '17 at 8:02
add a comment |Â
up vote
0
down vote
Using XMLStarlet:
$ xml ed -u '//fmreq:property[fmreq:name="object_name"]/preceding-sibling::fmreq:property/fmreq:name' -v YYZ file.xml
<?xml version="1.0"?>
<fmreq:fileManagementRequestDetail xmlns:fmreq="http://foobar.com/filemanagement">
<fmreq:property>
<fmreq:name>YYC</fmreq:name>
<fmreq:value>Memos</fmreq:value>
</fmreq:property>
<fmreq:property>
<fmreq:name>object_name</fmreq:name>
<fmreq:value>Correspondence</fmreq:value>
</fmreq:property>
</fmreq:fileManagementRequestDetail>
The first part of the XPath, //fmreq:property[fmreq:name="object_name"]
will locate the <fmreq:name>object_name</fmreq:name>
node, and the /preceding-sibling::fmreq:property/fmreq:name
bit will locate the <fmreq:name>
node of the preceding <fmreq:property>
node.
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
I think that there are a couple of problems in your sed
command:
You don't use the
-n
option, so by defaultsed
just prints every line of input to the output (possibly modified by ased
command).You don't need the redirection
< c3.xml
, becausesed
recognizes the last argument as a filename.sed
is not very well suited for matches over multiple lines. See for example here.
The following seems to work on your example:
sed -n "/<fmreq:name>object_name</fmreq:name>/ n;p" c3.xml | sed "s/^s*<fmreq:value>(.*)</fmreq:value>/1/g"
Or, with only one sed
invocation:
sed -n "/<fmreq:name>object_name</fmreq:name>/ n;s/^s*<fmreq:value>(.*)</fmreq:value>/1/g;p" c3.xml
Breakdown of what this command does:
The option
-n
tellssed
not to print the pattern space after it's finished processing the line. Consequently, you need to use the commandp
explicitely to do so./regex/
tellssed
to execute the commands that follow only on the lines that matchregex
.The
sed
commandn
replaces the content of the pattern space by the next line of input, which is the one containing the value you are interested in.The
sed
commands/regex/replacement/
substitutes the first match ofregex
in the pattern space byreplacement
.The
sed
commandp
prints the line.
Thanks, I'm still getting used to using sed so any obvious errors are mine and I appreciate any help given.
â Bob Lyman
Oct 15 '17 at 20:59
The redirection is a leftover from the original command as the operation has to eventually replace another value with a redirect to another file.
â Bob Lyman
Oct 15 '17 at 21:31
Glad it helped. But yes,sed
and regular expressions do take some time to get used to. :) All the best!
â Rastapopoulos
Oct 16 '17 at 8:02
add a comment |Â
up vote
2
down vote
accepted
I think that there are a couple of problems in your sed
command:
You don't use the
-n
option, so by defaultsed
just prints every line of input to the output (possibly modified by ased
command).You don't need the redirection
< c3.xml
, becausesed
recognizes the last argument as a filename.sed
is not very well suited for matches over multiple lines. See for example here.
The following seems to work on your example:
sed -n "/<fmreq:name>object_name</fmreq:name>/ n;p" c3.xml | sed "s/^s*<fmreq:value>(.*)</fmreq:value>/1/g"
Or, with only one sed
invocation:
sed -n "/<fmreq:name>object_name</fmreq:name>/ n;s/^s*<fmreq:value>(.*)</fmreq:value>/1/g;p" c3.xml
Breakdown of what this command does:
The option
-n
tellssed
not to print the pattern space after it's finished processing the line. Consequently, you need to use the commandp
explicitely to do so./regex/
tellssed
to execute the commands that follow only on the lines that matchregex
.The
sed
commandn
replaces the content of the pattern space by the next line of input, which is the one containing the value you are interested in.The
sed
commands/regex/replacement/
substitutes the first match ofregex
in the pattern space byreplacement
.The
sed
commandp
prints the line.
Thanks, I'm still getting used to using sed so any obvious errors are mine and I appreciate any help given.
â Bob Lyman
Oct 15 '17 at 20:59
The redirection is a leftover from the original command as the operation has to eventually replace another value with a redirect to another file.
â Bob Lyman
Oct 15 '17 at 21:31
Glad it helped. But yes,sed
and regular expressions do take some time to get used to. :) All the best!
â Rastapopoulos
Oct 16 '17 at 8:02
add a comment |Â
up vote
2
down vote
accepted
up vote
2
down vote
accepted
I think that there are a couple of problems in your sed
command:
You don't use the
-n
option, so by defaultsed
just prints every line of input to the output (possibly modified by ased
command).You don't need the redirection
< c3.xml
, becausesed
recognizes the last argument as a filename.sed
is not very well suited for matches over multiple lines. See for example here.
The following seems to work on your example:
sed -n "/<fmreq:name>object_name</fmreq:name>/ n;p" c3.xml | sed "s/^s*<fmreq:value>(.*)</fmreq:value>/1/g"
Or, with only one sed
invocation:
sed -n "/<fmreq:name>object_name</fmreq:name>/ n;s/^s*<fmreq:value>(.*)</fmreq:value>/1/g;p" c3.xml
Breakdown of what this command does:
The option
-n
tellssed
not to print the pattern space after it's finished processing the line. Consequently, you need to use the commandp
explicitely to do so./regex/
tellssed
to execute the commands that follow only on the lines that matchregex
.The
sed
commandn
replaces the content of the pattern space by the next line of input, which is the one containing the value you are interested in.The
sed
commands/regex/replacement/
substitutes the first match ofregex
in the pattern space byreplacement
.The
sed
commandp
prints the line.
I think that there are a couple of problems in your sed
command:
You don't use the
-n
option, so by defaultsed
just prints every line of input to the output (possibly modified by ased
command).You don't need the redirection
< c3.xml
, becausesed
recognizes the last argument as a filename.sed
is not very well suited for matches over multiple lines. See for example here.
The following seems to work on your example:
sed -n "/<fmreq:name>object_name</fmreq:name>/ n;p" c3.xml | sed "s/^s*<fmreq:value>(.*)</fmreq:value>/1/g"
Or, with only one sed
invocation:
sed -n "/<fmreq:name>object_name</fmreq:name>/ n;s/^s*<fmreq:value>(.*)</fmreq:value>/1/g;p" c3.xml
Breakdown of what this command does:
The option
-n
tellssed
not to print the pattern space after it's finished processing the line. Consequently, you need to use the commandp
explicitely to do so./regex/
tellssed
to execute the commands that follow only on the lines that matchregex
.The
sed
commandn
replaces the content of the pattern space by the next line of input, which is the one containing the value you are interested in.The
sed
commands/regex/replacement/
substitutes the first match ofregex
in the pattern space byreplacement
.The
sed
commandp
prints the line.
edited Oct 15 '17 at 9:06
answered Oct 15 '17 at 8:57
Rastapopoulos
518112
518112
Thanks, I'm still getting used to using sed so any obvious errors are mine and I appreciate any help given.
â Bob Lyman
Oct 15 '17 at 20:59
The redirection is a leftover from the original command as the operation has to eventually replace another value with a redirect to another file.
â Bob Lyman
Oct 15 '17 at 21:31
Glad it helped. But yes,sed
and regular expressions do take some time to get used to. :) All the best!
â Rastapopoulos
Oct 16 '17 at 8:02
add a comment |Â
Thanks, I'm still getting used to using sed so any obvious errors are mine and I appreciate any help given.
â Bob Lyman
Oct 15 '17 at 20:59
The redirection is a leftover from the original command as the operation has to eventually replace another value with a redirect to another file.
â Bob Lyman
Oct 15 '17 at 21:31
Glad it helped. But yes,sed
and regular expressions do take some time to get used to. :) All the best!
â Rastapopoulos
Oct 16 '17 at 8:02
Thanks, I'm still getting used to using sed so any obvious errors are mine and I appreciate any help given.
â Bob Lyman
Oct 15 '17 at 20:59
Thanks, I'm still getting used to using sed so any obvious errors are mine and I appreciate any help given.
â Bob Lyman
Oct 15 '17 at 20:59
The redirection is a leftover from the original command as the operation has to eventually replace another value with a redirect to another file.
â Bob Lyman
Oct 15 '17 at 21:31
The redirection is a leftover from the original command as the operation has to eventually replace another value with a redirect to another file.
â Bob Lyman
Oct 15 '17 at 21:31
Glad it helped. But yes,
sed
and regular expressions do take some time to get used to. :) All the best!â Rastapopoulos
Oct 16 '17 at 8:02
Glad it helped. But yes,
sed
and regular expressions do take some time to get used to. :) All the best!â Rastapopoulos
Oct 16 '17 at 8:02
add a comment |Â
up vote
0
down vote
Using XMLStarlet:
$ xml ed -u '//fmreq:property[fmreq:name="object_name"]/preceding-sibling::fmreq:property/fmreq:name' -v YYZ file.xml
<?xml version="1.0"?>
<fmreq:fileManagementRequestDetail xmlns:fmreq="http://foobar.com/filemanagement">
<fmreq:property>
<fmreq:name>YYC</fmreq:name>
<fmreq:value>Memos</fmreq:value>
</fmreq:property>
<fmreq:property>
<fmreq:name>object_name</fmreq:name>
<fmreq:value>Correspondence</fmreq:value>
</fmreq:property>
</fmreq:fileManagementRequestDetail>
The first part of the XPath, //fmreq:property[fmreq:name="object_name"]
will locate the <fmreq:name>object_name</fmreq:name>
node, and the /preceding-sibling::fmreq:property/fmreq:name
bit will locate the <fmreq:name>
node of the preceding <fmreq:property>
node.
add a comment |Â
up vote
0
down vote
Using XMLStarlet:
$ xml ed -u '//fmreq:property[fmreq:name="object_name"]/preceding-sibling::fmreq:property/fmreq:name' -v YYZ file.xml
<?xml version="1.0"?>
<fmreq:fileManagementRequestDetail xmlns:fmreq="http://foobar.com/filemanagement">
<fmreq:property>
<fmreq:name>YYC</fmreq:name>
<fmreq:value>Memos</fmreq:value>
</fmreq:property>
<fmreq:property>
<fmreq:name>object_name</fmreq:name>
<fmreq:value>Correspondence</fmreq:value>
</fmreq:property>
</fmreq:fileManagementRequestDetail>
The first part of the XPath, //fmreq:property[fmreq:name="object_name"]
will locate the <fmreq:name>object_name</fmreq:name>
node, and the /preceding-sibling::fmreq:property/fmreq:name
bit will locate the <fmreq:name>
node of the preceding <fmreq:property>
node.
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Using XMLStarlet:
$ xml ed -u '//fmreq:property[fmreq:name="object_name"]/preceding-sibling::fmreq:property/fmreq:name' -v YYZ file.xml
<?xml version="1.0"?>
<fmreq:fileManagementRequestDetail xmlns:fmreq="http://foobar.com/filemanagement">
<fmreq:property>
<fmreq:name>YYC</fmreq:name>
<fmreq:value>Memos</fmreq:value>
</fmreq:property>
<fmreq:property>
<fmreq:name>object_name</fmreq:name>
<fmreq:value>Correspondence</fmreq:value>
</fmreq:property>
</fmreq:fileManagementRequestDetail>
The first part of the XPath, //fmreq:property[fmreq:name="object_name"]
will locate the <fmreq:name>object_name</fmreq:name>
node, and the /preceding-sibling::fmreq:property/fmreq:name
bit will locate the <fmreq:name>
node of the preceding <fmreq:property>
node.
Using XMLStarlet:
$ xml ed -u '//fmreq:property[fmreq:name="object_name"]/preceding-sibling::fmreq:property/fmreq:name' -v YYZ file.xml
<?xml version="1.0"?>
<fmreq:fileManagementRequestDetail xmlns:fmreq="http://foobar.com/filemanagement">
<fmreq:property>
<fmreq:name>YYC</fmreq:name>
<fmreq:value>Memos</fmreq:value>
</fmreq:property>
<fmreq:property>
<fmreq:name>object_name</fmreq:name>
<fmreq:value>Correspondence</fmreq:value>
</fmreq:property>
</fmreq:fileManagementRequestDetail>
The first part of the XPath, //fmreq:property[fmreq:name="object_name"]
will locate the <fmreq:name>object_name</fmreq:name>
node, and the /preceding-sibling::fmreq:property/fmreq:name
bit will locate the <fmreq:name>
node of the preceding <fmreq:property>
node.
answered Oct 17 '17 at 9:52
Kusalananda
105k14209326
105k14209326
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%2f398157%2fsearch-replace-in-xml-file-with-sed-or-awk%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
1
XML processing is best done with a tool made for the job (bash and sed and awk are not made for the job).
â Jeff Schaller
Oct 14 '17 at 21:22
2
Where does "YYZ" come from?
â RobertL
Oct 14 '17 at 22:05
What's the format of the list file that holds the new values?
â seshoumara
Oct 14 '17 at 23:52
YYZ is just an example value (also the name of a Rush tune).
â Bob Lyman
Oct 15 '17 at 20:55
What's actually happening is the value from object_name is eval'd against a set of know values then the value under form_category_cd is replaced with that new value and the file written out. Yes I would love to not use sed and bash, but our operations group will not allow something like python or perl on the server because they don't have resources to support then. Believe this is a battle that was fought and lost by a co-worker.
â Bob Lyman
Oct 15 '17 at 20:58