Reading XML and replacing a string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reading XML and replacing a string
# 1  
Old 05-24-2012
Reading XML and replacing a string

Hi All,

My thanks in advance for you guys reading this and for any posts.

I'm having 100 XML files, I need script which replace a specific string.
It must be incrementing in 100 xml files..

Sample XML files:
<hwIPHostName type="attrib">DEMO1</hwIPHostName>

I need Demo1 to be incremented as DEMO2 in the next search likewise till DEMO100 of XML files. I've tried with Sed command but not working..

Can anyone help me out..
# 2  
Old 05-24-2012
Code:
perl -pe 's|(<hwIPHostName type="attrib">)(Demo\d+?)(</hwIPHostName>)|$1.uc($2).$3|eg' *.xml

Use -i switch perl -i -pe ...... for in-file editing.
# 3  
Old 05-24-2012
Hi balajesuri

Thanks for your reply.

But still it is not getting changed in the XML files. It shows me DEMO in all the files.

All files contain DEMO within that Tag. I need them to be incremented one by one in all the 100 files.
# 4  
Old 05-24-2012
Not sure I quite understand your question. The way I saw it was you wanted to convert lowercase "Demo" to uppercase.

Ok, you have 100 xml files. First file has a tag containing DEMO1 which you want it to be converted to DEMO2.

Likewise, you want to increment the number besides DEMO by 1, in each of the xml files.

Is this what you're looking for?

Code:
[root@host dir]# cat 1.xml
<hwIPHostName type="attrib">DEMO1</hwIPHostName>
[root@host dir]# cat 2.xml
<hwIPHostName type="attrib">DEMO2</hwIPHostName>
[root@host dir]# cat 3.xml
<hwIPHostName type="attrib">DEMO3</hwIPHostName>
[root@host dir]# perl -pe 's|(<hwIPHostName type="attrib">DEMO)(\d+?)(</hwIPHostName>)|$1.($2+1).$3|eg' *.xml
<hwIPHostName type="attrib">DEMO2</hwIPHostName>
<hwIPHostName type="attrib">DEMO3</hwIPHostName>
<hwIPHostName type="attrib">DEMO4</hwIPHostName>
[root@host dir]#

# 5  
Old 05-24-2012
You got it, It works it changes the value.

But for one change my files just contains the below in all the 100 files..

<hwIPHostName type="attrib">DEMO</hwIPHostName>

So i need that to be changed has the below order in all the other 100 files.

<hwIPHostName type="attrib">DEMO</hwIPHostName>
<hwIPHostName type="attrib">DEMO1</hwIPHostName>
<hwIPHostName type="attrib">DEMO2</hwIPHostName>
<hwIPHostName type="attrib">DEMO3</hwIPHostName>
..........................................................................
<hwIPHostName type="attrib">DEMO100</hwIPHostName>

I think you can do that..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

How to write in other language in text/xml file by reading english text/xml file using C++?

Hello Team, I have 2 files.one contains english text and another contains Japanese. so i have to read english text and replace the text with Japanesh text in third file. Basically, I need a help to write japanese language in text/xml file.I heard wstring does this.Not sure how do i write... (2 Replies)
Discussion started by: SA_Palani
2 Replies

2. UNIX for Dummies Questions & Answers

XML reading and parsing! Please help

See test.xml file attached (7 Replies)
Discussion started by: JulioAmerica
7 Replies

3. Shell Programming and Scripting

Replacing tab value in xml file

Hi I am working on xml file. I have to make sure below lines containing values within quotes are replaced by some character like "-". Below are different lines in xml file. Pattern 1: <Connector port=.... ..... keystoremyPass="xxx" /> Pattern 2: myword="xxxxx" ... (11 Replies)
Discussion started by: krsnadasa
11 Replies

4. Shell Programming and Scripting

Replacing a value in all xml's

Hi Folks, Could you please advise what will be the unix command to replace the character in all xml's under a particular directory for example let say I rite now at the following below location $ cd /opt/apr/rt/conf now under conf there are so many xml's and in those xml's i want to... (2 Replies)
Discussion started by: punpun66
2 Replies

5. Shell Programming and Scripting

Replacing part of XML code inside comment tags

Hello! I'd like to modify custom values in a XML config file between comment tags using bash script. <feature> <keyboardshortcut>C-m</keyboardshortcut> <option1>disabled</option2> <option2>enabled</option2> </feature> <!-- bash script features START --> <feature> ... (2 Replies)
Discussion started by: prism1
2 Replies

6. Shell Programming and Scripting

Reading a file and replacing char by position

Hi I'm looking for a way to read a text file that may contain 1000 records or more and each of these records has 460 characters. I need to read each record, and add a string of characters starting at position 256 for each record. Any suggestions using UNIX shell scripting. (4 Replies)
Discussion started by: macastor
4 Replies

7. Shell Programming and Scripting

Replacing the last record in xml with different tags

I have special requirement, my system provided the xml file as below(available xml file) and I need to convert it as below desired xml file. is it possible thru shell scripts or awk? What I need is : my available xml contains number of records with tags <RevenueAmounts>, the last of record is... (6 Replies)
Discussion started by: LinuxLearner
6 Replies

8. Shell Programming and Scripting

Reading only particular TAG from XML

Hi, I have an XML file with following structure. Between following tags I have pipedelimited records with newline characters (Data1|1|2|3) <!]> I need to read the data between above tags so that my output is a flat file with pipedelimited records. <BOS> <Header> <TTC>ABC</TTC> ... (9 Replies)
Discussion started by: dsrookie
9 Replies

9. Shell Programming and Scripting

replacing string in an XML file

Hi all, I need to replace string in XML file..XML file like <dependency> <groupId>fr.orange.portail.ear</groupId> <artifactId>_AdminServicesEAR</artifactId> <version>1.0.0-20080521.085352-1</version> <type>ear</type> </dependency> <dependency> ... (2 Replies)
Discussion started by: subin_bala
2 Replies

10. Shell Programming and Scripting

Need help reading XML file

If someone out there could help me out with this problem. I would really appreciate it. My requirement is :Read XML and put data in Test.txt file.Using Shell script(bourn) My xml file: <root> <header> <HeaderData1>header1</HeaderData1> <HeaderData2>header2</HeaderData2>... (0 Replies)
Discussion started by: ram2s2001
0 Replies
Login or Register to Ask a Question