Sed 正则表达式更改文件 [英] Sed regex change of file

查看:50
本文介绍了Sed 正则表达式更改文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我(未成功)尝试替换 Magento local.xml 文件(连接字符串文件)中的数据库主机条目.

I am (unsuccessfully) trying to substitute the database host entry in a Magento local.xml file (the connection string file).

该行如下:

<host><![CDATA[localhost]]></host>

我想用 sed 找到包含host"的行,然后用其他内容替换内括号的内容.

I want to find the line that contains "host" with sed and then replace the content of the inner brackets with something else.

注意 - 内括号的内容不会总是localhost",所以 s/localhost/lala/g 将不起作用.

Note - the content of the inner brackets won't always be "localhost", so s/localhost/lala/g won't work.

我得到了以下内容:

sed -i "/\<host\>/s/.../lala/2" local.xml

请帮忙.

推荐答案

使用 GNU sed:

With GNU sed:

sed 's|\(<host><!\[CDATA\[\).*\(\]\]></host>\)|\1lala\2|' file

sed -E 's|(<host><!\[CDATA\[).*(\]\]></host>)|\1lala\2|' file

输出:

<host><![CDATA[lala]]></host>

这篇关于Sed 正则表达式更改文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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