brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: xmlstarlet ed -L updates or deletes XML nodes in the file itself

Xml
~/tips/tip-xmlstarlet-ed-inplace

xmlstarlet ed -u '//item[@id="1"]/title' -v 'Changed' feed.xml

Change or delete one specific XML node in place, picked by XPath.

Changing one value in an XML file with sed works until the value appears twice. An XPath says exactly which node I mean.

xmlstarlet ed edits a document. -u updates the nodes an XPath matches and -v gives the new value. Without further options the result goes to the terminal, so you can check it first:

xmlstarlet ed -u '//item[@id="1"]/title' -v 'Changed' feed.xml

When the output looks right, add -L (long form --inplace) to write it back to the file. Deleting works the same way with -d:

xmlstarlet ed -L -d '//item[@id="2"]' feed.xml

A caveat: the file is rewritten by the parser, so it gains an XML declaration and may be re-indented. Keep a copy of the original if the exact layout matters, and use -P to preserve whitespace nodes.

Further reading: XMLStarlet user guide: editing XML

More Xml tips xmlstarlet sel -t -v '//item/title' -n feed.xml All Xml tips (2) All topics

Link