I have the following node in an XML document:
<client-version>1.2.8</client-version>
How can I replace replace the node's value, 1.2.8, with 1.2.9?
Wanted output:
<client-version>1.2.9</client-version>
I have the following node in an XML document:
<client-version>1.2.8</client-version>
How can I replace replace the node's value, 1.2.8, with 1.2.9?
Wanted output:
<client-version>1.2.9</client-version>
You would use an XML parser to do this. For example xmlstarlet (a command line XML tool):
$ xmlstarlet ed -u '//client-version' -v '1.2.9' file.xml
<?xml version="1.0"?>
<client-version>1.2.9</client-version>
The above command would locate all occurrences of the client-version document node and change their values to the string 1.2.9.
To only change the ones that are 1.2.8, you would use
xmlstarlet ed -u '//client-version[text() = "1.2.8"]' -v '1.2.9' file.xml
Redirect the output to a new file, inspect it and rename it to the original filename, or run xmlstarlet with its -L or --inplace options to edit the file in-place.
Using xq, from yq, from https://kislyuk.github.io/yq/, which allows you to use jq expressions to modify XML documents:
xq -x '(..|."client-version"? // empty) |= "1.2.9"' file.xml
This updates the value of each client-version node to 1.2.9 regardless of where in the document it is located.
The string 1.2.9 could be inserted from a variable like so:
new_version=1.2.9
xq -x --arg ver "$new_version" '(..|."client-version"? // empty) |= $ver' file.xml
Tried with below awk command and it worked fine
awk '$0 ~ /^<client-version>/ && $0 ~ /<\/client-version>/{gsub("1.2.8","1.2.9",$0)}1' orginalfile >temperorayfile && mv temperorayfile orginalfile