Positional Update of XML File


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Positional Update of XML File
# 1  
Old 10-23-2014
Wrench 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
Code:
<INTERCHANGE_CONTROL_NO>000000601</INTERCHANGE_CONTROL_NO>

in the XML file and change the first two 0 of the value to 9.

For instance
Code:
<INTERCHANGE_CONTROL_NO>000000601</INTERCHANGE_CONTROL_NO>

should be changed to
Code:
<INTERCHANGE_CONTROL_NO>990000601</INTERCHANGE_CONTROL_NO>

The position of the INTERCHANGE_CONTROL_NO is not fixed in the file. It can occur at various locations.
# 2  
Old 10-23-2014
Quote:
Originally Posted by Praveenkulkarni
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
Code:
<INTERCHANGE_CONTROL_NO>000000601</INTERCHANGE_CONTROL_NO>

in the XML file and change the first two 0 of the value to 9.

For instance
Code:
<INTERCHANGE_CONTROL_NO>000000601</INTERCHANGE_CONTROL_NO>

should be changed to
Code:
<INTERCHANGE_CONTROL_NO>990000601</INTERCHANGE_CONTROL_NO>

The position of the INTERCHANGE_CONTROL_NO is not fixed in the file. It can occur at various locations.
Hello Praveen,

Following may help you.
Code:
awk '/INTERCHANGE_CONTROL_NO/ {sub(/00/,"99",$1)} 1'  Input_file

Thanks,
R. Singh
# 3  
Old 10-23-2014
Gawk
Code:
gawk '$0 ~ tag{
		print gensub("(<"tag">)(.*)(<\\/"tag">)","\\1"new"\\3","g")
		next
     }1' tag='INTERCHANGE_CONTROL_NO' new="99999" file

Sed
Code:
#!/bin/sh
tag="INTERCHANGE_CONTROL_NO"
value="something"
file="file"

sed  "s|\(<$tag>\)[^<>]*\(</$tag>\)|\1$value\2|" $file

# 4  
Old 10-23-2014
Thanks for the quick response.

I tried the below:
Code:
awk '/INTERCHANGE_CONTROL_NO/ {sub(/00/,"99",$1)} 1'  Input_file

and this did not work.

The sed command is completely replacing the value with 99. However, I wanted only the first 2 00 of the value to be changed to 99 as listed below:

Code:
<INTERCHANGE_CONTROL_NO>000000601</INTERCHANGE_CONTROL_NO>

should be changed to

Code:
<INTERCHANGE_CONTROL_NO>990000601</INTERCHANGE_CONTROL_NO>

Can this be achieved with a perl command so that my input file is updated rather than moving the updated data to another file.

Again thanks a lot for all your support.
# 5  
Old 10-23-2014
My bad I thought you want to replace with new value try this

Code:
#!/bin/sh
tag="INTERCHANGE_CONTROL_NO"
value="99"
file="file"

sed  "s|\(<$tag>\)..\(.*\)\(</$tag>\)|\1$value\2\3|" $file

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

Parsing a file based on positional constraints

I have a list file1 like dog cow fox cat fish duck crowI want to classify the elements of file1 based on constrains applied on file2. Additionally the number of elements (words) in the each line of file2 is not fixed. This is my file2 cow cat fox dog cow fox dog fish crow fox dog cat ... (5 Replies)
Discussion started by: sammy777
5 Replies

4. Shell Programming and Scripting

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: <!-- some text here> . . . <message:test name="ryan"> <message:sample-channel charset="UTF-8" max-value="2" wait="20"> ... (5 Replies)
Discussion started by: brichigo
5 Replies

5. 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

6. 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

7. 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

8. Shell Programming and Scripting

Split positional flat file.

Hi, I need to split positional flat file, based on value at position 43-45.( in red "410") Example: 12345678907886421689 200920184820410200920020092002007 12345678907886421689 200920184820411200920020092002007 12345678907886421689 200920184820411200920020092002007... (6 Replies)
Discussion started by: meetmedude
6 Replies

9. Shell Programming and Scripting

Get the positional value from first line of the file

Hi, I have one flat file with delimited field as pipe(|) symbol. The file contains header,detail lines. Header is the first line in the file. I want to read the value for the position from 15 to 18 in first line of the file. Pls help me to get the value from position 15 to 18 in... (3 Replies)
Discussion started by: praka
3 Replies

10. Shell Programming and Scripting

How to update data between xml tags

Is there a way to modify Non Null data between <host> and </host> tags to a new value ?- may be using sed/awk? I tried this sed 's|.*<host>\(?*\)</host>.*|\<host>xxx</host>|' but it is updating the host which has null value - want opposite of this - Thanks in advance for you help!! For... (2 Replies)
Discussion started by: harry_todd
2 Replies
Login or Register to Ask a Question