sed search and replace after xml tag


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
# 1  
Question sed search and replace after xml tag

Hi All,
I'm new to sed.

In following XML file
Code:
   <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>
    <interface type='direct'>
      <mac address='52:54:00:ab:d5:77'/>
      <source dev='eno2' mode='bridge'/>
      <model type='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
    </interface>

i have add one new xml tag after <source dev='eno1' mode='bridge'/>, which is <target dev='New1'/>.

Code:
for i in `eno1 eno2 eno3 eno4`
do
        echo "Updating macvtap device names for $i"
	sed -i.bak "/source dev='$i'/a <target dev='New$cnt'/>" 
$XML_FILE
    	cnt=`expr $cnt + 1`
done

The above is not working. Please let me know how to do it?



Moderator's Comments:
Mod Comment Please use code tags as required by forum rules!

Last edited by RudiC; 06-03-2016 at 11:50 AM.. Reason: Added code tags.
# 2  
Do you have in mind that eno# is not the only form of Ethernet device names? eno# is only the naming for onboard network cards.

It can be too be a name like ens1 or ens3f0 ore a few other combinations. See

PredictableNetworkInterfaceNames
# 3  
Yes, i'm aware of that.

All i wanted is give a persistent name to the macvtap devices using this script.

I have taken care of finding the interface names, now only thing have to do is in that xml, <source dev='eno1' mode='bridge/>, append the next line with specific target dev names, using this sed command.
# 4  
You want to cycle the for loop through each given word
Code:
for i in eno1 eno2 eno3 eno4
do

Optimization: your shell certainly does not need an external call of expr for integer arithmetics
Code:
 cnt=$((cnt+=1))

# 5  
to clarify, the following is command substition(call a command and get the output of it).

Code:
`eno1 eno2 eno3 eno4`

As you just want the bare words, omit the apostrophe.

And for the nit-picker in me, this works to too:

Code:
((cnt++))

This User Gave Thanks to stomp For This Post:
# 6  
Do you want to use a counter (which, BTW, you did not initialize and thus is the empty string for the first loop) or the source device's number?

Code:
for i in eno1 eno2 eno3 eno4
    do    echo "Updating macvtap device names for $i"
          sed "/source dev='$i'/a <target dev='New${i#${i%?}}'/>"  XML_file
    done

This User Gave Thanks to RudiC For This Post:
# 7  
Code:
${i#${i%?}}

Interesting construct Smilie Another way of expressing...

Code:
${i: -1}

...and i just got confused with ...

Code:
${i:-1}

which does not work as desired because :- is default value assignment.
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 Science: Computers
Difficulty: Medium
To bypass US Munitions Export Laws, the creator of the PGP published all the source code in book form.
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. How to Post in the The UNIX and Linux Forums

How to replace value of password tag in xml with blanks when special characters are there?

Hi All, I am trying to replace the values inside <password> tag in an xml file but it doesn't replace certain passwords: For eg: Server/home/sperinc>cat TextXML.txt <appIds> <entry name="AccountXref"> <type id="ldap"> <realm>nam</realm> ... (7 Replies)
Discussion started by: saroopkris85
7 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

Need to replace XML TAG

As per the requirement I need to replace XML tag with old to new on one of the XML file. Old<com : DEM>PHI</com : DEM> New<com : DEM>PHM</com : DEM> Please someone provide the sed command to replace above mentioned old XML tag with new XML tag (2 Replies)
Discussion started by: siva83
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. 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

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

8. Shell Programming and Scripting

Need an efficient way to search for a tag in an xml file having millions of rows

Hi, I have an XML file with around 1 billion rows in it and i am trying to find the number of times a particular tag occurs in it. The solution i am using works but takes a lot of time (~1 hr) .Please help me with an efficient way to do this. Lets say the input file is <Root> ... (13 Replies)
Discussion started by: Sheel
13 Replies

9. Shell Programming and Scripting

Search for word in a xml file and replace it with something else

Hello Unix Users, I am very new to Unix so I am not sure how do I do the following. I need a script such that when I type the following in the command prompt > . scriptName.sh wordToBeReplaced DirectoryLocation will find the word someword located in a somefile.xml in DirectoryLocation... (8 Replies)
Discussion started by: 5211171
8 Replies

10. Shell Programming and Scripting

Sed command to clean xml tag

Hi, 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>" |... (3 Replies)
Discussion started by: iamwha1am
3 Replies

Featured Tech Videos