Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用sed替换xml值_Xml_Bash_Sed - Fatal编程技术网

用sed替换xml值

用sed替换xml值,xml,bash,sed,Xml,Bash,Sed,我想替换XML文件中的值。示例XML <Console> <!-- REQUIRED PARAMETERS --> <!-- Enter the node that you are running the installation program from. This value must be a fully qualified name, which includes a host name and

我想替换XML文件中的值。示例XML

<Console>
                <!-- REQUIRED PARAMETERS -->
                <!-- Enter the node that you are running the installation program from. This value must be a fully qualified name, which includes a host name and domain name. For example, node001.server.name.com.-->
                <node>node.sample.ibm.com</node>

                <!-- Enter the domain name of the cluster as a single dot-separated string. For example, server.name.com. -->
        <sso-domain-name>sample.ibm.com</sso-domain-name>

        <!-- Set this property to true if your InfoSphere BigInsights Console uses HTTPS. Otherwise, enter false. -->
                <https>false</https>

        <!-- management-console-port specifies which port is used by the Management Console. -->
        <management-console-port>8080</management-console-port>

node.sample.ibm.com
sample.ibm.com
假的
8080
我想换个值。 我试着跟随,但不起作用。谢谢你的帮助

sed -i "/<Console>/,/<\/Console>/ s/<node>[0-9][a-z]\+/<node>newvalue
sed-i/,//s/[0-9][a-z]\+/newvalue

使用合适的XML解析器。例如,下面是我将要做的:

sed-i-e'//,//s |[0-9a-z.]\{1,\}newvalue | g'YourFile
我假设值介于标记和之间,并且仅包含小写字母、点和数字(基于您的示例并重试)。如果扩展,只需添加类中缺少的字符,如
[0-9a-z.\u-A-z::
,可能还有子类[:空格:]


如果
newvalue
是一个可变内容,请不要忘记使用双引号和转义字符
&
\
以及sed
s
操作的分隔符(默认值为
//code>和
,在我的代码中)

简单的XML可以很好地与套件一起处理

您可以将XML转换为基于行的格式,并按照您习惯的方式进行处理。例如:

xml2 < x.xml | perl -pe 's|^(/Console/node=)(.*)$|$1newvalue|g' | 2xml > y.xml
xml2y.xml

我不太精通sed,我相信您可以轻松地用sed替换perl。

您可以使用xmlstarlet:

xmlstarlet ed -u //Console/node -v 'new value'

(或类似的东西)

这不是你问题的答案。但是你也可以在命令行上使用“xmllint”。这可以直接处理XML结构。不要。@Wintermute for simple XML工作得很好。这不是重复,因为Q要求
sed
非常有趣。但是我更喜欢一个无perl的解决方案。非常有趣。但我更喜欢perl-less solution;-)@choroba很好。但是我对sed的能力更差:-)谢谢NeronLeVelu,它起作用了是的newvalue是一个变量,在这种情况下会怎么样?如果newvalue不包含&\,只需在sed操作中将周围的简单引号替换为双引号。如果它包含那些字符,在字符串中使用它之前将它们转义,并使用d双倍引用了很多NeronLeVelu的话,它奏效了。
xml2 < x.xml | perl -pe 's|^(/Console/node=)(.*)$|$1newvalue|g' | 2xml > y.xml
xmlstarlet ed -u //Console/node -v 'new value'