sed replace xml tag

后端 未结 2 1613
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-23 23:19

I want to replace value inside a tag in an xml file using sed.

 xxxx-SS 

I want to replace xxxx-SS with some she

相关标签:
2条回答
  • 2021-01-23 23:48

    You may try to use awk, it would more simple to achieve what you desire,

    $ cp test.xml test_orig.xml
    
    $ awk '/<version> xxxx-SS <\/version>/{gsub(/<version> xxxx-SS <\/version>/,"<version> $ver </version>",$0)}1' test_orig.xml > test.xml
    $ cat test.xml
    ...
    <version> $ver </version>
    ...
    
    $ rm test_orig.xml
    

    The command would substitute xxxx-SS to test (modify the var value to what you want)

    0 讨论(0)
  • 2021-01-23 23:51

    Using xml/html parsers is the right way to manipulate xml/html documents. Don't use sed/awk tools for such cases.

    xmlstarlet solution:

    xmlstarlet ed -u "//version[1]" -v $ver test.xml
    
    • ed - edit mode

    • -u - update action

    • //version[1] - xpath expression to select the first version tag

    • -v $ver - the new value for selected node

    0 讨论(0)
提交回复
热议问题