在XML在bash更换标签 [英] Replacing tags in xml in bash

查看:113
本文介绍了在XML在bash更换标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个XML文件是格式如下:

I have an xml file that is of the following format:

<list>
    <version>1.5</version>
    <version>1.4</version>
    <version>1.3</version>
    <version>1.2</version>
</list>

我们的想法是,我总是更新了新版本的第一个版本标记。当我这样做,我代替随后的标签。

The idea is that I always update the first version tag with a new version. And when I do so, I replace the subsequent tags.

例如,当我更新了1.6版本的第一个标签(我知道怎么做),下面的标签是:

For example, when I update the 1.6 version as the first tag (which I know how to do), the following tags would be:

<list>
    <version>1.6</version>
    <version>1.5</version>
    <version>1.4</version>
    <version>1.3</version>  
</list>

我试图让两个选项去。

I've tried to get two options going.

第一个选项:
我的preferred选择是搜索XML文件,并替换版本标记I + 1版本标记我。是这样的:

First Option: My preferred option would be to search the xml file and replace the version tag i+1 with version tag i. Something like:

sed -E '2,/<version>.*<\/version>/s#<version>(.*)</c>#<version>\1</version>#' file.xml

我在哪里搜索版本的二审和版本的第一个实例(目前没有工作)。

Where I search for the second instance of version and replace it with the first instance of version (currently not working).

第二个选项:
我的第二个选项是版本标签存储在像变量:

Second Option: My second option would be to store the version tags in variables like:

version=$(grep -oPm1 "(?<=version>)[^<]+" file.xml)
version2=$(grep -oPm2 "(?<=version>)[^<]+" file.xml)

然后,通过第1版替换版本2,做替换:

Then replace version 2 by version 1 and do the replacement:

sed -i "s/${version2}/${version}/g" file.xml

不过,这种选择提供了:

However, this options gives:

的sed:-e前pression#1,烧焦9:未结束的'命令

sed: -e expression #1, char 9: unterminated 's' command.

当我尝试:

sed -i "/$version2/s/${version2}/${version}/g" file.xml

我得到:

未结束的地址正则表达式

unterminated address regex

显然,对于任一选项的想法是把code在一个循环,这样我可以我的时间运行它。但是,我坚持,我都试过选项不起作用。

Obviously, the idea for either option would be to put the code in a loop so that I can run it i times. However, I am stuck and both options I've tried don't work.

推荐答案

Don't使用文字处理工具,如 AWK SED 使用XML,如果你能在所有的避免它。虽然这个特定子集可能是这样简单,使这种方法是可行的,手头上有合适的工具将避免头痛以后(如果文件格式被延长;如果有人添加评论到前面;等等)。

Don't use text-manipulation tools such as awk or sed to work with XML if you can at all avoid it. While this specific subset may be so simple as to make the approach feasible, having the right tools at hand will avoid headaches later (if the file format gets extended; if someone adds comments to the front; etc).

new_version=1.6
xmlstarlet ed \
  -d '/list/version[last()]' \
  -i '/list/version[1]' -t elem -n version -v "$new_version" \
  <old.xml >new.xml


  • -d'/列表/版[最后()] 删除最后版本中的列表条目

  • -i'/列表/版[1]-t -n ELEM版本-v 1.6 引入/ 版本<命名的新元素code>,其值为 1.6 ,在目前由第一个版本所持的立场。

    • -d '/list/version[last()]' deletes the last version entry in the list.
    • -i '/list/version[1]' -t elem -n version -v 1.6 introduces a new element named version, with the value 1.6, in the position currently held by the very first version.
    • 这篇关于在XML在bash更换标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆