DEV Community

Bojan Jagetic
Bojan Jagetic

Posted on • Edited on • Originally published at bojanjagetic.com

Update xml file via Terminal or bash script

Introduction

There are several ways you can update an XML file using the terminal or a bash script. Here are a few options:

sed

Use sed: sed is a command-line tool that can be used to search and replace text in a file. You can use it to update an XML file by specifying the search pattern and the replacement text. For example:



sed -i 's/<old-text>/<new-text>/g' file.xml


Enter fullscreen mode Exit fullscreen mode

This will search for all instances of old-text in file.xml and replace them with new-text.

awk

Use awk: awk is a programming language designed for text processing. You can use it to update an XML file by specifying the search pattern and the replacement text. For example:



awk '/<old-text>/{sub("<old-text>","<new-text>")}1' file.xml > newfile.xml


Enter fullscreen mode Exit fullscreen mode

This will search for all instances of old-text in file.xml and replace them with new-text, and write the updated content to a new file called newfile.xml.

xmlstarlet

Use xmlstarlet: xmlstarlet is a command-line tool for working with XML files. You can use it to update an XML file by specifying the element you want to update and the new value. For example:



xmlstarlet ed -L -u '/root/element/@attribute' -v 'new value' file.xml


Enter fullscreen mode Exit fullscreen mode

This will update the attribute of the element element under the root element in file.xml with the value new value.

Note: These are just a few examples of how you can update an XML file using the terminal or a bash script. There are many other tools and techniques you can use, depending on your specific needs.

Combination of sed and xmlstarlet

To update an XML file from a bash script, you can use a combination of the sed command and the xmlstarlet utility. Here is an example of how this can be done:

First, make sure that the sed and xmlstarlet utilities are installed on your system.

Create a bash script and open it in a text editor.

Use the sed command to find and replace a specific string in the XML file. For example, the following command will replace the string "old_string" with "new_string" in the file "input.xml":



sed 's/old_string/new_string/g' input.xml > output.xml


Enter fullscreen mode Exit fullscreen mode

Use the xmlstarlet utility to update specific elements or attributes in the XML file. For example, the following command will update the value of the name attribute in the item element to new_name:



xmlstarlet ed -u '/items/item/@name' -v 'new_name' input.xml > output.xml


Enter fullscreen mode Exit fullscreen mode

Save and close the bash script.

Run the bash script to update the XML file.

Keep in mind that this is just a basic example, and you can use a combination of sed and xmlstarlet commands to update the XML file in more complex ways. You can also use other utilities such as awk or perl to manipulate the XML file in your bash script.

Checkout original post

Update xml file via Terminal or bash script

If you been in situation that you want to update xml file from bash script there is many ways to do that.

bojanjagetic.com

Top comments (0)