Sed command to clean xml tag

Login or Register for Dates, Times and to Reply

Thread Tools Search this Thread
# 1  
Sed command to clean xml tag

Can someone help me come up with a generic sed command to clean a tag off its attributes?

For eg.
Input String - <tag attrib=new>This String</tag>
should undergo a sed transformation to get
Output String - <tag >This String</tag>

This works -
echo "<tag attrib=new>This</tag>" | sed -e 's/\([^<\/A-Za-z]\)[^>]*>/\1>/g'

<tag >This</tag>

However, the moment i add a space in there - it goes for a toss
echo "<tag attrib=new>This String</tag>" | sed -e 's/\([^<\/A-Za-z]\)[^>]*>/\1>/g'

<tag >This >

Resolved the above issue using a single replace -
echo "<tag attrib=new>This String</tag>" | sed -e 's/\([^<\/A-Za-z]\)[^>]*>/\1>/'

<tag >This String</tag>

However, it does not handle the scenario -
echo " <tag attrib=new>This String</tag>" | sed -e 's/\([^<\/A-Za-z]\)[^>]*>/\1>/'

>This String</tag>

Any inputs on this will be of great help.

Last edited by iamwha1am; 12-30-2008 at 06:28 PM..
# 2  


echo "<tag attrib=new>This lousy winter day</tag>" \
    | sed -e 's#<\([^ ]*\)[^>]*>\([^<]*\)\(</\1>\)#<\1>\2\3#g'

<tag>This lousy winter day</tag>

HTH Chris
# 3  
Wow. That approach works great. Thanks Chris.
Also, can you help me understand - how could i have changed the original approach to take care of this - 'cos i have a feeling that i was nearly there (or maybe not!!)
# 4  
Simply the "g" at the end of the sed command. You want sed
to only execute one substitution, the first.

Your corrected code:
echo "<tag attrib=new>This String</tag>" \
    | sed -e 's/\([^<\/A-Za-z]\)[^>]*>/\1>/'

<tag >This String</tag>

Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #385
Difficulty: Easy
The Linux uptime command show how long the hard drive has been powered down.
True or False?

10 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. Shell Programming and Scripting

sed search and replace after xml tag

Hi All, I'm new to sed. In following XML file <interface type='direct'> <mac address='52:54:00:86:ce:f6'/> <source dev='eno1' mode='bridge'/> <model type='virtio'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/> </interface> ... (8 Replies)
Discussion started by: varunrapelly
8 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. 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

5. Shell Programming and Scripting

sed substition within XML tag

Hi all, I basically want to remove certain characters from within a certain XML tag: From: <mytagone>hello 1-2-3 world</mytagone> <mytagtwo>hello 1-2-3 world</mytagtwo> To: <mytagone>hello 1 2 3 world</mytagone> <mytagtwo>hello 1-2-3 world</mytagtwo> Is this possible using sed... (6 Replies)
Discussion started by: Jedimark
6 Replies

6. Shell Programming and Scripting

Using shell command need to parse multiple nested tag value of a XML file

I have this XML file - <gp> <mms>1110012</mms> <tg>988</tg> <mm>LongTime</mm> <lv> <lkid>StartEle=ONE, Desti = Motion</lkid> <kk>12</kk> </lv> <lv> <lkid>StartEle=ONE, Source = Velocity</lkid> <kk>2</kk> </lv> <lv> ... (3 Replies)
Discussion started by: NeedASolution
3 Replies

7. Shell Programming and Scripting

awk command to insert a tag in XML

Hi All, I need a help on inserting a XML tag. Actual input <var> <nam>abcd</nam> <a1>.</a1> </var> if tag <a1>.</a1> is getting missed in XML like below <var> <nam>abcd</nam> </var> i need to insert wherever it is missed after <nam> tag and before </var> tag. Could anyone... (3 Replies)
Discussion started by: mohanalakshmi
3 Replies

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

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

10. Shell Programming and Scripting

shell command to remove some XML tag is needed

Hi all, I have a file which i have to remove some line from it, the lines that i have to remove from my file is as below: </new_name></w"s" langue="Fr-fr" version="1.0" encoding="UTF-8" ?> <New_name> and it is finding at the middle of my file, is there any command line in linux to do it or do... (10 Replies)
Discussion started by: id_2pc
10 Replies

Featured Tech Videos