Search code examples
xmlbashshellxmllint

Removing a line from XML file using xmllint in shell script


I have a xml file in a particular directory now I want to remove a particular entry (see example) from the file and update the existing xml file using shell script.

Example: (See code) I have to remove line 4 (entry of a particular resource)

<?xml version="1.0" encoding="UTF-8"?>
<manifest:manifest xmlns:manifest="....." manifest:version="1.2">
 <manifest:file-entry manifest:full-path="/" manifest:version="1.2" manifest:media-type="..."/>
"line to be deleted" <manifest:file-entry manifest:full-path="...." manifest:media-type="..."/>
 ......some more lines....
 </manifest:manifest>

Help will be appreciated :)
I am new at this and not much aware about editing a xml file, from some sources I have found that xmllint can be used but I am not sure about that.


Solution

  • As far as I know, xmllint is not made for editing XML files, so you can't do that.


    One possible solution to your problem is command:

    grep -v your_pattern your_file > new_file

    It will choose all other lines than the one and copy it into a new file.


    Another, probably the better way, is using of xmlstarlet (documentation).

    You can use it like this:

    xmlstarlet ed --delete your_xpath input.xml > output.xml