how to delete line from the XML file

5,928

Solution 1

Try this:

perl -i -pe 's|^<units>.*</Networks>$||' /my/filename

Note that if you have leading or trailing space in the line you will need this instead:

perl -i -pe 's|^ *<units>.*</Networks> *$||' /my/filename

I uesd pipe as a separator rather than slash to avoid unnecessary escaping.

Solution 2

Do not use regular expressions to parse XML. It's an excellent way to create brittle code, because there's a bunch of perfectly valid things you can do with XML which will break a regex. Things like reformatting the XML in entirely valid ways (such as 'pretty printing' it in a nested/indented form) will break your code.

Instead I would suggest - use an XML parser. Personally, I like the XML::Twig module in perl.

Your comment suggests that what you're trying to do is add stuff to a <Networks> element in your XML.

So how about something like this:

#!/usr/bin/perl
use strict;
use warnings;

use XML::Twig;

my $xml_text = '<XML>
<Networks><units><unit ip="1.2.3.4" /></units><ranges/></Networks>
</XML>';

my $parser = XML::Twig->new( 'pretty_print' => 'indented' );
#would probably use 'parsefile' instead here
$parser->parse($xml_text);

print "\nBefore:\n";
$parser->print;


#insert a new element into 'Networks':
$parser->root->first_child('Networks')->insert_new_elt(
        'last_child',     #position - end of "Networks" element
        'new_element',    #element
        {   'attribute_here'    => "value_here",
            'another_attribute' => 'another_value',
        },                 #attributes as key value pairs
        "Content_here",    #element content
    );


print "\nAfter:\n";
$parser->print;
Share:
5,928

Related videos on Youtube

maihabunash
Author by

maihabunash

I am 17 years old and love to develop

Updated on September 18, 2022

Comments

  • maihabunash
    maihabunash over 1 year

    how to delete only the line from xml file ( with sed/awk or perl one liner line )

    that start with:

    <units> 
    

    and ended with

    </Networks>
    

    as the follwoing

    <units><unit ip= ............   </units><ranges/></Networks>
    
    • wurtel
      wurtel about 9 years
      You understand that this leaves you with an xml file that is syntactically incorrect, i.e. you're missing the closing </Networks> tag by removing the line.
    • maihabunash
      maihabunash about 9 years
      yes I know that ( the first Networks exist on the top of the file
    • maihabunash
      maihabunash about 9 years
      dont worry later I will append new lines that will be in place that line
    • Sobrique
      Sobrique about 9 years
      Looks like an XY problem to me. What are you -actually- trying to accomplish? Parsing XML with regex is a bad idea, because there's lots of ways it can go wrong. Creating malformed XML deliberately is also a bad idea, for much the same reason. What problem are you trying to solve? I will probably suggest the answer is - use an XML parser, not a regex. With a bit more XML as a sample, this is actually very easy to do.
    • Sobrique
      Sobrique about 9 years
      X posted to Stack Overflow stackoverflow.com/questions/29078801/…
  • Dan Sheppard
    Dan Sheppard about 9 years
    I'll now leave you to be partonised by the regulars: if you're an expert, you already know the issues, and if not you will learn them by trying this.
  • maihabunash
    maihabunash about 9 years
    the units line is only the one unit line in the XML so I not see any problem here
  • Admin
    Admin about 9 years
    The awk one is a really (no pun intended) awkward way of doing it. Why not awk '!/<units>.*<\/Networks>/'
  • Admin
    Admin about 9 years
    You need the ! before it or else it will only print that line
  • terdon
    terdon about 9 years
    @JID fixed, I wrote too fast
  • Admin
    Admin about 9 years
    Sorry, you are also missing the last / :(
  • terdon
    terdon about 9 years
    @JID yes, that's what I get for editing on my phone. Thanks. Next time though, please edit the post directly to fix this sort of thing.
  • Admin
    Admin about 9 years
    Sorry :( Last time i edited someones post they weren't happy at all. Will do though :)