使用sed更新xml属性(Windows + Cygwin和Linux)? [英] Update xml attribute using sed (Windows + cygwin and Linux)?

查看:77
本文介绍了使用sed更新xml属性(Windows + Cygwin和Linux)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用sed命令对xml文件进行更新,但是我遇到了麻烦.它需要同时在Windows(使用cygwin)和Linux上运行.

I need to make an update on an xml file using a sed command and I'm having trouble with this one.. the it needs to run on both Windows (using cygwin) and Linux.

XML具有以下元素:

The XML has the following element:

...

<parameter name="jersey.test.host" value="localhost" />

...

我需要将值更新为任意值,例如"abc".

I need to update the value to an arbitrary value, say, "abc".

我尝试了一些命令,例如:

I tried several commands such as:

sed -i "s#\(jersey.test.host\)\s*value="\(.*\)" />$#\1abc#" "C:\file.xml"

还有:

sed -i "s#<parameter name="jersey.test.host" value=".*" />#<parameter name="jersey.test.host" value="abc" />#"" "C:\file.xml"

但是当我运行命令时,文件没有执行任何更改,我敢打赌这意味着我的匹配不正确.

But no changes are performed to the file when I run the command, which I bet means I'm not matching correctly.

我不介意我是否也可以使用awk来做到这一点.无论什么方法,只要在Windows + Cygwin和Linux中都可以使用相同的命令即可.

I would not mind if I could do this using awk as well. Whatever works, as long as the same command can be used in both Windows + Cygwin and Linux.

有人可以帮我这个忙吗?任何指针都将不胜感激!

Can anyone help me out with this one? Any pointers are greatly appreciated!

谢谢!

推荐答案

如果sed命令包含双引号,则应将其括在单引号中(或转义双引号).

If your sed command includes double-quotes, you should enclose it in single-quotes (or escape double-quotes).

以下命令应完成以下工作:

Following command should do the work:

sed -i 's#<parameter name="jersey.test.host" value=".*" />#<parameter name="jersey.test.host" value="abc" />#' "C:\file.xml"

这篇关于使用sed更新xml属性(Windows + Cygwin和Linux)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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