4

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.

4
  • It's good that you don't want to do this with line-based tools like grep or sed, since XML is not flat, but the question doesn't make it clear what exactly you want to remove (other than that it is a manifest:file-entry tag). What are the criteria? Commented May 20, 2015 at 13:23
  • I have to remove a particular file from a zip package to save the space. Since I have deleted the file directly from the shell script, now I want to remove the file entry from the manifest file to ensure proper functioning. Commented May 20, 2015 at 13:30
  • @Beginner I'm not sure if I understand you correctly. So, did you use my advice or something else? And what exactly happened? Commented May 20, 2015 at 13:56
  • I have used grep or xmlstarlet it worked for me Commented May 20, 2015 at 14:26

1 Answer 1

5

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
Sign up to request clarification or add additional context in comments.

2 Comments

If you're content with this answer, you can accept it to make this question marked as "solved" :-)
I upvoted the solution using xmlscarlet, not the one using grep

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.