Find and update line in xml file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find and update line in xml file
# 1  
Old 10-21-2014
Find and update line in xml file

Hi,

I have a xml file that I need to modify 1 line to change some value from 2 to 10 (or any number).

Sample input:
Code:
<!-- some text here>
.
.
.
<message:test name="ryan">
     <message:sample-channel charset="UTF-8" max-value="2" wait="20">
         <message:sample>test123</message:sample>
     </message:sample-channel>
</message:test>

Basically, I only need to update the max-value from 2 to any number (i.e. 10). However, there are multiple occurrences of the ff line in the xml.
Code:
<message:sample-channel charset="UTF-8" max-value="2" wait="20">

The only unique line that can be used as pattern is '<message:test name="ryan">'

Is there a sed or awk command that can be used to update the max-value?
# 2  
Old 10-21-2014
Quote:
Originally Posted by brichigo
Hi,

I have a xml file that I need to modify 1 line to change some value from 2 to 10 (or any number).

Sample input:
Code:
<!-- some text here>
.
.
.
<message:test name="ryan">
     <message:sample-channel charset="UTF-8" max-value="2" wait="20">
         <message:sample>test123</message:sample>
     </message:sample-channel>
</message:test>

Basically, I only need to update the max-value from 2 to any number (i.e. 10). However, there are multiple occurrences of the ff line in the xml.
Code:
<message:sample-channel charset="UTF-8" max-value="2" wait="20">

The only unique line that can be used as pattern is '<message:test name="ryan">'

Is there a sed or awk command that can be used to update the max-value?
Hello brichigo,

Following may help, lets say our input file is as follows.

Code:
cat test14
<message:test name="ryan">
     <message:sample-channel charset="UTF-8" max-value="2" wait="20">
         <message:sample>test123</message:sample>
     </message:sample-channel>
</message:test>
<message:test name="ryan">
     <message:sample-channel charset="UTF-8" max-value="3" wait="20">
         <message:sample>test123</message:sample>
     </message:sample-channel>
</message:test>
<message:test name="ryan">
     <message:sample-channel charset="UTF-8" max-value="5" wait="20">
         <message:sample>test123</message:sample>
     </message:sample-channel>
</message:test>

Then code is as follows.
Code:
awk '/<message:test name="ryan">/ {print $0;getline;gsub(/[[:digit:]]/,"10",$3);} 1' test14

Output will be as follows.
Code:
<message:test name="ryan">
<message:sample-channel charset="UTF-8" max-value="10" wait="20">
         <message:sample>test123</message:sample>
     </message:sample-channel>
</message:test>
<message:test name="ryan">
<message:sample-channel charset="UTF-8" max-value="10" wait="20">
         <message:sample>test123</message:sample>
     </message:sample-channel>
</message:test>
<message:test name="ryan">
<message:sample-channel charset="UTF-8" max-value="10" wait="20">
         <message:sample>test123</message:sample>
     </message:sample-channel>
</message:test>

Just taken example as per your guven input. So kindly let me know if this helps.

Thanks,
R. Singh
# 3  
Old 10-21-2014
Thanks Ravinder! I will try this and will let you know if it helps.
# 4  
Old 10-21-2014
Code:
akshay@nio:/tmp$ cat file.xml
<!-- some text here>
.
.
.
<message:test name="ryan">
     <message:sample-channel charset="UTF-8" max-value="2" wait="20">
         <message:sample>test123</message:sample>
     </message:sample-channel>
</message:test>

Code:
akshay@nio:/tmp$ sed -E 's@(.*)(max-value)="[^"]*"@\1\2="any number"@g' file.xml


Resulting

Code:
<!-- some text here>
.
.
.
<message:test name="ryan">
     <message:sample-channel charset="UTF-8" max-value="any number" wait="20">
         <message:sample>test123</message:sample>
     </message:sample-channel>
</message:test>

# 5  
Old 10-24-2014
Quote:
Originally Posted by RavinderSingh13
Hello brichigo,

Following may help, lets say our input file is as follows.

Code:
cat test14
<message:test name="ryan">
     <message:sample-channel charset="UTF-8" max-value="2" wait="20">
         <message:sample>test123</message:sample>
     </message:sample-channel>
</message:test>
<message:test name="ryan">
     <message:sample-channel charset="UTF-8" max-value="3" wait="20">
         <message:sample>test123</message:sample>
     </message:sample-channel>
</message:test>
<message:test name="ryan">
     <message:sample-channel charset="UTF-8" max-value="5" wait="20">
         <message:sample>test123</message:sample>
     </message:sample-channel>
</message:test>

Then code is as follows.
Code:
awk '/<message:test name="ryan">/ {print $0;getline;gsub(/[[:digit:]]/,"10",$3);} 1' test14

Output will be as follows.
Code:
<message:test name="ryan">
<message:sample-channel charset="UTF-8" max-value="10" wait="20">
         <message:sample>test123</message:sample>
     </message:sample-channel>
</message:test>
<message:test name="ryan">
<message:sample-channel charset="UTF-8" max-value="10" wait="20">
         <message:sample>test123</message:sample>
     </message:sample-channel>
</message:test>
<message:test name="ryan">
<message:sample-channel charset="UTF-8" max-value="10" wait="20">
         <message:sample>test123</message:sample>
     </message:sample-channel>
</message:test>

Just taken example as per your guven input. So kindly let me know if this helps.

Thanks,
R. Singh
Hi Ravinder,

It is working as expected. Thank you.

One more question, how can I add leading white spaces on the same line so that the xml format is still the same as before?

---------- Post updated at 08:42 PM ---------- Previous update was at 08:39 PM ----------

Quote:
Originally Posted by Akshay Hegde
Code:
akshay@nio:/tmp$ cat file.xml
<!-- some text here>
.
.
.
<message:test name="ryan">
     <message:sample-channel charset="UTF-8" max-value="2" wait="20">
         <message:sample>test123</message:sample>
     </message:sample-channel>
</message:test>

Code:
akshay@nio:/tmp$ sed -E 's@(.*)(max-value)="[^"]*"@\1\2="any number"@g' file.xml


Resulting

Code:
<!-- some text here>
.
.
.
<message:test name="ryan">
     <message:sample-channel charset="UTF-8" max-value="any number" wait="20">
         <message:sample>test123</message:sample>
     </message:sample-channel>
</message:test>

Thanks Akshay but -E option is not available in our UNIX machine. We're using AIX.
# 6  
Old 10-25-2014
The -E is only a nicety, it can be dropped if the `()' are escaped.
e.i
Code:
sed 's@\(max-value="\)[0-9]*"@\1any number"@' file.xml

The space you mentioned is preserved

And so it can be done with Perl as well
Code:
perl -wlpe 's/(max-value=")\d+/$1any_number/' file.xml

---------- Post updated at 09:05 PM ---------- Previous update was at 08:28 PM ----------

In case you have similar lines containing max-value that you don't want to touch, here's another solution in Perl, preserving indentation as well.
Code:
perl -wlpe '$p and s/(max-value=")\d+/$1NUMBER/; $p = /<message:test name="ryan">/' file.xml


Last edited by Aia; 10-25-2014 at 12:23 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Update particular tag in a XML file

Below is the content in my XML file <name>XXX</name> <eventType>Uptime</eventType> <eventType>Delay</eventType> <eventType>Delay</eventType> <name>YYY</name> <eventType>Uptime</eventType> <eventType>Delay</eventType> ... (12 Replies)
Discussion started by: Viswanatheee55
12 Replies

2. Shell Programming and Scripting

How to retrieve values from XML file and update them in the same position! PLEASE HELP?

Good Day All Im quiet new to ksh scripting and need a bit of your help. I am attempting to write a script that reads in an XML and extracts certain field values from an XML file. The values are all alphanumeric and consist of two components: e.g "Test 1". I need to to create a script that... (2 Replies)
Discussion started by: JulioAmerica
2 Replies

3. Shell Programming and Scripting

Positional Update of XML File

Hello, I have a XML file and need to update the data for a specific XML Attribute in the file. I need a Perl or Awk command to look for <INTERCHANGE_CONTROL_NO>000000601</INTERCHANGE_CONTROL_NO> in the XML file and change the first two 0 of the value to 9. For instance ... (4 Replies)
Discussion started by: Praveenkulkarni
4 Replies

4. Shell Programming and Scripting

Script need to do update xml file

<avp name="CC-Request-Type" value="1"> </avp> <avp name="CC-Request-Number" value="0"> </avp> <avp name="Subscription-Id"> <avp name="Subscription-Id-Type" value="0"></avp> <avp name="Subscription-Id-Data" value="4081234567"></avp> </avp> <avp... (5 Replies)
Discussion started by: gstar
5 Replies

5. Shell Programming and Scripting

Find out special characters from xml file

Hi....I have a xml file which is having lots of special characters which I need to find out and put the distinct list of those into a text file. The list of special characters is not specific, it can be anything at different point of time. Can anyone help me to find out the same and list out? ... (10 Replies)
Discussion started by: Krishanu Saha
10 Replies

6. Shell Programming and Scripting

Find Node and replace line(s) preceding in xml file

Hello, I have an xml file whose contacts are like below: <Node>Apple <B>Value1</B> <B>Value2</B> <B>Value3</B> </Node> <Node>Mango <B>Value1</B> <B>Value2</B> <B>Value3</B> </Node> <Node>Apple <B>Value1</B> <B>Value2</B> <B>Value3</B> </Node> <Node>Bannana (3 Replies)
Discussion started by: umarsatti
3 Replies

7. Shell Programming and Scripting

Find/Replace in XML file

I am not sure how to approach this find/replace using a shell script. Any help or guidance appreciated. I want to find this: <objects/> <thumb>thumb_0001.jpg</thumb> <preview>preview_0001.jpg</preview> And replace with something like this: <objects>... (1 Reply)
Discussion started by: jimraynor
1 Replies

8. Shell Programming and Scripting

perl script to update a xml file

Hi experts, I have a set of xml files in folder which has the below field. <mm:sessionID>157.235.206.12900397BE4:A</mm:sessionID>, I need to update this field regularly with new session id, which I have it from a login file. Can anyone tell me how to add a new value in <mm:sessionID>... (3 Replies)
Discussion started by: amvarma77
3 Replies

9. Shell Programming and Scripting

How to find spaces and update values on the same line.

Dear all, I'm trying to write a script where: A file contains more or less 2000 lines. To some of those lines, in a specific position, let's say 89-92 there are spaces. So, this script should find these spaces on specific position and update a value (from 2 to 1) to another position of the... (4 Replies)
Discussion started by: paniklas
4 Replies

10. Shell Programming and Scripting

Perl: update lastmod in xml file

I'm trying to write a perl script that I can run as a cron job in root of my web server that will look for .shtml files get their last modified date and replace it in the sitemap_test.xml file. the problem is the substitution doesn't work and when I print to MYFILE it adds the lastmod to the end of... (3 Replies)
Discussion started by: skilodge
3 Replies
Login or Register to Ask a Question