Change value inside xml attribute using sed?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I have the following attribute:



<client-version>1.2.8</client-version>


How can I replace everything inside <client-version></client-version> to only change the value (1.2.8)?



So it would be:



<client-version>1.2.9</client-version>









share|improve this question






















  • Related: Replace an XML attribute's value with the value of a shell variable

    – steeldriver
    Mar 15 at 14:46

















0















I have the following attribute:



<client-version>1.2.8</client-version>


How can I replace everything inside <client-version></client-version> to only change the value (1.2.8)?



So it would be:



<client-version>1.2.9</client-version>









share|improve this question






















  • Related: Replace an XML attribute's value with the value of a shell variable

    – steeldriver
    Mar 15 at 14:46













0












0








0








I have the following attribute:



<client-version>1.2.8</client-version>


How can I replace everything inside <client-version></client-version> to only change the value (1.2.8)?



So it would be:



<client-version>1.2.9</client-version>









share|improve this question














I have the following attribute:



<client-version>1.2.8</client-version>


How can I replace everything inside <client-version></client-version> to only change the value (1.2.8)?



So it would be:



<client-version>1.2.9</client-version>






shell-script shell sed xml






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 15 at 14:28









Cathal Mac DonnachaCathal Mac Donnacha

31




31












  • Related: Replace an XML attribute's value with the value of a shell variable

    – steeldriver
    Mar 15 at 14:46

















  • Related: Replace an XML attribute's value with the value of a shell variable

    – steeldriver
    Mar 15 at 14:46
















Related: Replace an XML attribute's value with the value of a shell variable

– steeldriver
Mar 15 at 14:46





Related: Replace an XML attribute's value with the value of a shell variable

– steeldriver
Mar 15 at 14:46










2 Answers
2






active

oldest

votes


















6














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.






share|improve this answer























  • I'm currently using bamboo to run this script as a task, bamboo is running CENTOS and when I use xmlstarlet it says the command is not found. Hence why I went down the sed route.

    – Cathal Mac Donnacha
    Mar 15 at 15:38












  • Running it as a shell script on bamboo. CentOS 6. Would xmlstartlet be available on this? Do I need to install it first? Apologies I'm a bit new to it.

    – Cathal Mac Donnacha
    Mar 15 at 15:48







  • 1





    @CathalMacDonnacha See, for example, here: How to instal/setup XMLStarlet in Linux?

    – Kusalananda
    Mar 15 at 17:33











  • Thanks for that!

    – Cathal Mac Donnacha
    Mar 19 at 13:37


















0














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 





share|improve this answer


















  • 2





    It might work fine once, but it's wrong, so don't use it in production code. It only takes tiny insignificant variations in the formatting of the XML to break it. When working with XML, always use XML-aware tools.

    – user32929
    Mar 15 at 16:42











  • @user32929 I'm trying to run this as a shell script on bamboo. CentOS 6. Would xmlstartlet be available on this? Do I need to install it first? Apologies I'm a bit new to it.

    – Cathal Mac Donnacha
    Mar 15 at 16:47












Your Answer








StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f506526%2fchange-value-inside-xml-attribute-using-sed%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









6














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.






share|improve this answer























  • I'm currently using bamboo to run this script as a task, bamboo is running CENTOS and when I use xmlstarlet it says the command is not found. Hence why I went down the sed route.

    – Cathal Mac Donnacha
    Mar 15 at 15:38












  • Running it as a shell script on bamboo. CentOS 6. Would xmlstartlet be available on this? Do I need to install it first? Apologies I'm a bit new to it.

    – Cathal Mac Donnacha
    Mar 15 at 15:48







  • 1





    @CathalMacDonnacha See, for example, here: How to instal/setup XMLStarlet in Linux?

    – Kusalananda
    Mar 15 at 17:33











  • Thanks for that!

    – Cathal Mac Donnacha
    Mar 19 at 13:37















6














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.






share|improve this answer























  • I'm currently using bamboo to run this script as a task, bamboo is running CENTOS and when I use xmlstarlet it says the command is not found. Hence why I went down the sed route.

    – Cathal Mac Donnacha
    Mar 15 at 15:38












  • Running it as a shell script on bamboo. CentOS 6. Would xmlstartlet be available on this? Do I need to install it first? Apologies I'm a bit new to it.

    – Cathal Mac Donnacha
    Mar 15 at 15:48







  • 1





    @CathalMacDonnacha See, for example, here: How to instal/setup XMLStarlet in Linux?

    – Kusalananda
    Mar 15 at 17:33











  • Thanks for that!

    – Cathal Mac Donnacha
    Mar 19 at 13:37













6












6








6







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.






share|improve this answer













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.







share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 15 at 15:07









KusalanandaKusalananda

141k18263439




141k18263439












  • I'm currently using bamboo to run this script as a task, bamboo is running CENTOS and when I use xmlstarlet it says the command is not found. Hence why I went down the sed route.

    – Cathal Mac Donnacha
    Mar 15 at 15:38












  • Running it as a shell script on bamboo. CentOS 6. Would xmlstartlet be available on this? Do I need to install it first? Apologies I'm a bit new to it.

    – Cathal Mac Donnacha
    Mar 15 at 15:48







  • 1





    @CathalMacDonnacha See, for example, here: How to instal/setup XMLStarlet in Linux?

    – Kusalananda
    Mar 15 at 17:33











  • Thanks for that!

    – Cathal Mac Donnacha
    Mar 19 at 13:37

















  • I'm currently using bamboo to run this script as a task, bamboo is running CENTOS and when I use xmlstarlet it says the command is not found. Hence why I went down the sed route.

    – Cathal Mac Donnacha
    Mar 15 at 15:38












  • Running it as a shell script on bamboo. CentOS 6. Would xmlstartlet be available on this? Do I need to install it first? Apologies I'm a bit new to it.

    – Cathal Mac Donnacha
    Mar 15 at 15:48







  • 1





    @CathalMacDonnacha See, for example, here: How to instal/setup XMLStarlet in Linux?

    – Kusalananda
    Mar 15 at 17:33











  • Thanks for that!

    – Cathal Mac Donnacha
    Mar 19 at 13:37
















I'm currently using bamboo to run this script as a task, bamboo is running CENTOS and when I use xmlstarlet it says the command is not found. Hence why I went down the sed route.

– Cathal Mac Donnacha
Mar 15 at 15:38






I'm currently using bamboo to run this script as a task, bamboo is running CENTOS and when I use xmlstarlet it says the command is not found. Hence why I went down the sed route.

– Cathal Mac Donnacha
Mar 15 at 15:38














Running it as a shell script on bamboo. CentOS 6. Would xmlstartlet be available on this? Do I need to install it first? Apologies I'm a bit new to it.

– Cathal Mac Donnacha
Mar 15 at 15:48






Running it as a shell script on bamboo. CentOS 6. Would xmlstartlet be available on this? Do I need to install it first? Apologies I'm a bit new to it.

– Cathal Mac Donnacha
Mar 15 at 15:48





1




1





@CathalMacDonnacha See, for example, here: How to instal/setup XMLStarlet in Linux?

– Kusalananda
Mar 15 at 17:33





@CathalMacDonnacha See, for example, here: How to instal/setup XMLStarlet in Linux?

– Kusalananda
Mar 15 at 17:33













Thanks for that!

– Cathal Mac Donnacha
Mar 19 at 13:37





Thanks for that!

– Cathal Mac Donnacha
Mar 19 at 13:37













0














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 





share|improve this answer


















  • 2





    It might work fine once, but it's wrong, so don't use it in production code. It only takes tiny insignificant variations in the formatting of the XML to break it. When working with XML, always use XML-aware tools.

    – user32929
    Mar 15 at 16:42











  • @user32929 I'm trying to run this as a shell script on bamboo. CentOS 6. Would xmlstartlet be available on this? Do I need to install it first? Apologies I'm a bit new to it.

    – Cathal Mac Donnacha
    Mar 15 at 16:47
















0














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 





share|improve this answer


















  • 2





    It might work fine once, but it's wrong, so don't use it in production code. It only takes tiny insignificant variations in the formatting of the XML to break it. When working with XML, always use XML-aware tools.

    – user32929
    Mar 15 at 16:42











  • @user32929 I'm trying to run this as a shell script on bamboo. CentOS 6. Would xmlstartlet be available on this? Do I need to install it first? Apologies I'm a bit new to it.

    – Cathal Mac Donnacha
    Mar 15 at 16:47














0












0








0







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 





share|improve this answer













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 






share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 15 at 16:22









Praveen Kumar BSPraveen Kumar BS

1,7751311




1,7751311







  • 2





    It might work fine once, but it's wrong, so don't use it in production code. It only takes tiny insignificant variations in the formatting of the XML to break it. When working with XML, always use XML-aware tools.

    – user32929
    Mar 15 at 16:42











  • @user32929 I'm trying to run this as a shell script on bamboo. CentOS 6. Would xmlstartlet be available on this? Do I need to install it first? Apologies I'm a bit new to it.

    – Cathal Mac Donnacha
    Mar 15 at 16:47













  • 2





    It might work fine once, but it's wrong, so don't use it in production code. It only takes tiny insignificant variations in the formatting of the XML to break it. When working with XML, always use XML-aware tools.

    – user32929
    Mar 15 at 16:42











  • @user32929 I'm trying to run this as a shell script on bamboo. CentOS 6. Would xmlstartlet be available on this? Do I need to install it first? Apologies I'm a bit new to it.

    – Cathal Mac Donnacha
    Mar 15 at 16:47








2




2





It might work fine once, but it's wrong, so don't use it in production code. It only takes tiny insignificant variations in the formatting of the XML to break it. When working with XML, always use XML-aware tools.

– user32929
Mar 15 at 16:42





It might work fine once, but it's wrong, so don't use it in production code. It only takes tiny insignificant variations in the formatting of the XML to break it. When working with XML, always use XML-aware tools.

– user32929
Mar 15 at 16:42













@user32929 I'm trying to run this as a shell script on bamboo. CentOS 6. Would xmlstartlet be available on this? Do I need to install it first? Apologies I'm a bit new to it.

– Cathal Mac Donnacha
Mar 15 at 16:47






@user32929 I'm trying to run this as a shell script on bamboo. CentOS 6. Would xmlstartlet be available on this? Do I need to install it first? Apologies I'm a bit new to it.

– Cathal Mac Donnacha
Mar 15 at 16:47


















draft saved

draft discarded
















































Thanks for contributing an answer to Unix & Linux Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f506526%2fchange-value-inside-xml-attribute-using-sed%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown






Popular posts from this blog

How to check contact read email or not when send email to Individual?

Bahrain

Postfix configuration issue with fips on centos 7; mailgun relay