sed xml file multiple line replacement


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed xml file multiple line replacement
# 1  
Old 07-15-2009
sed xml file multiple line replacement

I have a file called config.xml, it's a simple xml file, and I need use sed/awk to erase some lines.

Code:
  <machine xsi:type="unix-machineType">
    <name>server1</name>
    <node-manager>
      <name>server1</name>
      <listen-address>server1</listen-address>
    </node-manager>
  </machine>
  <machine xsi:type="unix-machineType">
    <name>server2</name>
  </machine>

That occurrs in the config.xml file, and I need to delete only the first occurrence, this part needs to be deleted:

Code:
  <machine xsi:type="unix-machineType">
    <name>server1</name>
    <node-manager>
      <name>server1</name>
      <listen-address>server1</listen-address>
    </node-manager>
  </machine>

so I'm only left with

Code:
  <machine xsi:type="unix-machineType">
    <name>server2</name>
  </machine>

----edit----
Code:
grep -A6 -m1 '<machine xsi*' config.xml

That gets me the exact result I want to remove

Last edited by cbo0485; 07-15-2009 at 04:23 PM..
# 2  
Old 07-15-2009
Try this:
Code:
awk 'BEGIN{f=1}
/<machine xsi:type="unix-machineType">/{f--}
!f && /<\/machine>/{f--;next}
f' file

Regards
# 3  
Old 07-15-2009
Quote:
Originally Posted by Franklin52
Try this:
Code:
awk 'BEGIN{f=1}
/<machine xsi:type="unix-machineType">/{f--}
!f && /<\/machine>/{f--;next}
f' file

Regards
Works perfect. There any way that can be done w/o piping it to another file, and just perform the command on the existing file? The script I'm going to add this to already backs it up so no need to name it to a tmp tile and then rename it again.
# 4  
Old 07-15-2009
yes and no - a 'poor man' edit file in 'situ':
Code:
{ rm FILE; awk'...' > FILE; } < FILE

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to pull multiple XML tags from the same XML file in Shell.?

I'm searching for the names of a TV show in the XML file I've attached at the end of this post. What I'm trying to do now is pull out/list the data from each of the <SeriesName> tags throughout the document. Currently, I'm only able to get data the first instance of that XML field using the... (9 Replies)
Discussion started by: hungryd
9 Replies

2. UNIX for Beginners Questions & Answers

Grepping multiple XML tag results from XML file.

I want to write a one line script that outputs the result of multiple xml tags from a XML file. For example I have a XML file which has below XML tags in the file: <EMAIL>***</EMAIL> <CUSTOMER_ID>****</CUSTOMER_ID> <BRANDID>***</BRANDID> Now I want to grep the values of all these specified... (1 Reply)
Discussion started by: shubh752
1 Replies

3. Shell Programming and Scripting

Multiple Replacement in a Text File in one operation (sed/awk) ?

Hi all, Saying we have two files: 1. A "Reference File" whose content is "Variable Name": "Variable Value" 2. A "Model File" whose content is a model program in which I want to substitute "VariableName" with their respective value to produce a third file "Program File" which would be a... (4 Replies)
Discussion started by: dae
4 Replies

4. Shell Programming and Scripting

Split xml file into multiple xml based on letterID

Hi All, We need to split a large xml into multiple valid xml with same header(2lines) and footer(last line) for N number of letterId. In the example below we have first 2 lines as header and last line as footer.(They need to be in each split xml file) Header: <?xml version="1.0"... (5 Replies)
Discussion started by: vx04
5 Replies

5. Shell Programming and Scripting

Splitting XML file on basis of line number into multiple file

Hi All, I have more than half million lines of XML file , wanted to split in four files in a such a way that top 7 lines should be present in each file on top and bottom line of should be present in each file at bottom. from the 8th line actual record starts and each record contains 15 lines... (14 Replies)
Discussion started by: ajju
14 Replies

6. Shell Programming and Scripting

sed replacement in file when line is in a variable

Hi, I have a file where I want to replace the 15th field separated by comma, only on specific lines matching lots of different conditions. I have managed to read the file line by line, within the loop my line is held in a variable called $line I assume this will be using sed (maybe... (5 Replies)
Discussion started by: jpt123
5 Replies

7. Shell Programming and Scripting

Scripting question, replacement in xml file

Hi Anybody can help me to make a script that replace string "Su saldo es" with "Your balance" in XML block if it start with Text Id=98 and Text Id= 12 only sample of part of file below <Text Id="98"> <Language id="1">Su saldo es $mainAccountBalance1Tiene ademas $dedicatedAccount1Balance1... (13 Replies)
Discussion started by: Ashu_099
13 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

XML tag replacement from different XML file

We have 2 XML file 1. ORIGINAL.xml file and 2. ATTRIBUTE.xml files, In the ORIGINAL.xml we need some modification as <resourceCode>431048</resourceCode>under <item type="Manufactured"> tag - we need to grab the 431048 value from tag and pass it to database table in unix shell script to find the... (0 Replies)
Discussion started by: balrajg
0 Replies

10. Shell Programming and Scripting

HELP Need in SED/PERL conditional line replacement

Hi , I need some help on perl/sed conditional replacement The situation is like below . I have a file contents like below . AAA|BBB|CCC|DDD AAA|BCF|CCC|HHH AAA|BVF|JJJ|KKK Here in the above file . I know my second column value (taking "|" as my delimited ) Basically I have to... (3 Replies)
Discussion started by: robin.r888
3 Replies
Login or Register to Ask a Question