sed:删除两个字符串之间的字符 [英] sed: delete characters between two strings

查看:124
本文介绍了sed:删除两个字符串之间的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用sed删除"foo = 1&"之间的所有字符和"bar = 2&"对于xml文件中的所有匹配项.

I'd like to use sed to remove all characters between "foo=1&" and "bar=2&" for all occurrences in an xml file.

<url>http://example.com?addr=123&foo=1&s=alkjasldkffjskdk$bar=2&f=jkdng</url>
<url>http://example.com?addr=124&foo=1&k=d93ndkdisnskiisndjdjdj$bar=2&p=dnsks</url>

这是我的sed命令:

sed -e '/foo=1&/,/bar=2&/d' sample.xml

运行此文件时,文件未更改.

When I run this, the file is unchanged.

以上内容基于此示例:查找"string1"并在该字符串与"string2"

The above is based on this example: Find "string1" and delete between that and "string2"

推荐答案

使用 substitution 命令代替 delete 命令:

sed -e 's/\(foo=1&\).*\(bar=2&\)/\1\2/'

这篇关于sed:删除两个字符串之间的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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