Find the answer to your Linux question:
Results 1 to 3 of 3
i need to delete a block of text containing a regex for instance I have a xml file which is: <item> <key>166</key> <value>x</value> </item> <item> <key>200</key> <value>y</value> </item> i need ...
  1. #1
    Just Joined!
    Join Date
    Feb 2010
    Posts
    14

    sed command

    i need to delete a block of text containing a regex

    for instance I have a xml file which is:
    <item>
    <key>166</key>
    <value>x</value>
    </item>
    <item>
    <key>200</key>
    <value>y</value>
    </item>

    i need to delete the item(block of text between <item> and </item> containing the key 166 so how can I make use of sed here
    Can someone please provide the sed command to be used?

  2. #2
    Linux Newbie
    Join Date
    Apr 2010
    Location
    Novosibirsk, Russia
    Posts
    136
    Quote Originally Posted by prad87 View Post
    i need to delete a block of text containing a regex

    for instance I have a xml file which is:
    <item>
    <key>166</key>
    <value>x</value>
    </item>
    <item>
    <key>200</key>
    <value>y</value>
    </item>

    i need to delete the item(block of text between <item> and </item> containing the key 166 so how can I make use of sed here
    Can someone please provide the sed command to be used?
    You should build a regexp that looks like

    's#<item><key>166</key>.*</item>##g'

    where instead of .* you should suggest an appropriate template (this could be a bit more complex, then just one sigil :) ).
    I advise you to read regexp tutorials in Perldoc for details :)

  3. #3
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    You may be able to write a regular expression to do this for specific and simple cases, but general XML parsing and understanding cannot be done with regular expressions. Your best bet would be to use a full XML parser that could delete the necessary elements and create a new XML document.
    DISTRO=Arch
    Registered Linux User #388732

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...