Substitute partial data only within a xml tag


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Substitute partial data only within a xml tag
# 1  
Old 01-15-2012
Substitute partial data only within a xml tag

Hello,

I have huge xml files and I need to replace only part of the data within a particular xml tag. This doesnt seem to be as simple as it sounds. I have searched everywhere and couldnt find any solution.

Ex:
In the below case I would like "def" to be replaced by "xyz" only when found in tag2 and not anywhere else.

sample line in xml file:
<tag1>def ghi</tag1><tag2>abc def ghi</tag2><tag3>def jkl</tag3>

desired output:
<tag1>def ghi</tag1><tag2>abc xyz ghi</tag2><tag3>def jkl</tag3>
# 2  
Old 01-15-2012
Using pointer,any programming language can do this.
But it's dangerous if you do the job incorrectly,it's possible to damage data.

First to find out the line you want to replace,then build new line with same length,last write new line to file in corresponding position.

You can googleSmilieython file pointer.Beacause i used python.
# 3  
Old 01-15-2012
Code:
perl -pe 's:(<tag2>.*?)def(.*?</tag2>):$1xyz$2:g' input.xml

This User Gave Thanks to balajesuri For This Post:
# 4  
Old 01-15-2012
Hi

Thank you Balaji! I was actually looking for a solution using sed. is that possible? However, the above perl option is working as expected. Can you please explain the command by breaking it?
# 5  
Old 01-15-2012
Code:
sed 's:\(<tag2>.*\)def\(.*<\/tag2>\):\1xyz\2:g' input.xml

Part to be substituted:
\(<tag2>.*\) => Searches for <tag2> and characters after it and stores it in \1. (note that this greedily matches for all characters after <tag2>)
def => Tries to look for 'def'. But since .* would have already matched all the characters after <tag2>, it will give up characters one by one till it matches 'def'.
\(.*<\/tag2>\) => Searches for characters that end with </tag2> and hold it in \2.


Part that will be replaced:
\1xyz\2 => Print value in \1 followed by xyz followed by \2.

To summarise: Search for string starting with '<tag2>' followed by some characters followed by def followed by some more characters, followed by </tag2>.. Now replace only 'def' in searched pattern by 'xyz'.

Last edited by balajesuri; 01-15-2012 at 01:20 AM..
# 6  
Old 01-15-2012
hi

Thank you. A nice trick using back references however, if there are two "def" words within the same tag then the substitution to xyz would happen only for the second def. The first def in the tag will be ignored and wont be replaced with xyz. Any solution to this?
# 7  
Old 01-15-2012
Code:
$ cat input
<tag1>def ghi</tag1><tag2>abc def def</tag2><tag3>def jkl</tag3>
<tag1>def ghi</tag1><tag2>abc def ghi</tag2><tag3>def jkl</tag3>
$
$ perl -pe 's/(<tag2>.*?<\/tag2>)//g;$x=$1;$x=~s/def/xyz/g;s/(<\/tag1>)/$1$x/' input
<tag1>def ghi</tag1><tag2>abc xyz xyz</tag2><tag3>def jkl</tag3>
<tag1>def ghi</tag1><tag2>abc xyz ghi</tag2><tag3>def jkl</tag3>

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Moving XML tag/contents after specific XML tag within same file

Hi Forum. I have an XML file with the following requirement to move the <AdditionalAccountHolders> tag and its content right after the <accountHolderName> tag within the same file but I'm not sure how to accomplish this through a Unix script. Any feedback will be greatly appreciated. ... (19 Replies)
Discussion started by: pchang
19 Replies

2. UNIX for Dummies Questions & Answers

How to substitute for the partial match?

Hi I have a question and hope I can get answer here. Thank you in advance. I have two files: file1: aa X bb Y cc Z file2: cc A bb B dd C aa D bb E If the 1st column match in both file1 and file2, the 2nd column in file2 will be replaced by the 2nd column in file1. If there is no... (2 Replies)
Discussion started by: yuejian
2 Replies

3. Shell Programming and Scripting

To search for a particular tag in xml and collate all similar tag values and display them count

I want to basically do the below thing. Suppose there is a tag called object1. I want to display an output for all similar tag values under heading of Object 1 and the count of the xmls. Please help File: <xml><object1>house</object1><object2>child</object2>... (9 Replies)
Discussion started by: srkmish
9 Replies

4. UNIX for Dummies Questions & Answers

Partial match in two files then substitute

Hi, I was trying to figure this out but failed so I hope someone here can help me, thank you in advance. I have two files. file1: aa M bb N cc O dd P ee Q file2: aa A_87_P254063 cc A_87_P016532 bb A_87_P104793 dd A_87_P055331 ee A_87_P059706 aa A_87_P071636 ee A_87_P028302... (2 Replies)
Discussion started by: yuejian
2 Replies

5. Shell Programming and Scripting

XML Parse between to tag with upper tag

Hi Guys Here is my Input : <?xml version="1.0" encoding="UTF-8"?> <xn:MeContext id="01736"> <xn:VsDataContainer id="01736"> <xn:attributes> <xn:vsDataType>vsDataMeContext</xn:vsDataType> ... (12 Replies)
Discussion started by: pareshkp
12 Replies

6. UNIX for Advanced & Expert Users

Shell Script to read XML tags and the data within that tag

Hi unix Gurus, I am really new to Unix Scripting. Please help me to create a shell script which reads the xml file and from that i need to fetch a particular information. For example <SOURCE BUSINESSNAME ="" DATABASETYPE ="Teradata" DBDNAME ="DWPROD3" DESCRIPTION ="" NAME... (2 Replies)
Discussion started by: SmilePlease
2 Replies

7. Shell Programming and Scripting

How to add the multiple lines of xml tags before a particular xml tag in a file

Hi All, I'm stuck with adding multiple lines(irrespective of line number) to a file before a particular xml tag. Please help me. <A>testing_Location</A> <value>LA</value> <zone>US</zone> <B>Region</B> <value>Russia</value> <zone>Washington</zone> <C>Country</C>... (0 Replies)
Discussion started by: mjavalkar
0 Replies

8. Shell Programming and Scripting

How to retrieve the value from XML tag whose end tag is in next line

Hi All, Find the following code: <Universal>D38x82j1JJ </Universal> I want to retrieve the value of <Universal> tag as below: Please help me. (3 Replies)
Discussion started by: mjavalkar
3 Replies

9. Shell Programming and Scripting

How To get the data from a tag in XML File

Hi I have a XML file in which data is loaded from a relational table and the column names are tags in the xml file which is shown below. ... (8 Replies)
Discussion started by: naughty21
8 Replies
Login or Register to Ask a Question