sed正则表达式中的布尔值OR [英] Boolean OR in sed regex

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

问题描述

我正在尝试替换配置文件中名为boots的软件包的所有引用.

I'm trying to replace all references of a package named boots in a configuration file.

行格式为add fast (package OR pkg) boots-(any-other-text),例如:

add fast package boots-2.3
add fast pkg boots-4.5

我要替换为:

add fast pkg boots-5.0

我尝试了以下sed命令:

sed -e 's/add fast (pkg\|package) boots-.*/add yinst pkg boots-5.0/g'
sed -e 's/add fast [pkg\|package] boots-.*/add yinst pkg boots-5.0/g'

什么是正确的正则表达式?我认为我在布尔值(packagepkg)部分中缺少某些内容.

What's the right regex? I think I'm missing something in the boolean or (package or pkg) part.

推荐答案

sed -e 's/add fast \(pkg\|package\) boots-.*/add yinst pkg boots-5.0/g'

您总是可以通过两次操作来避免OR

You could always avoid the OR by doing it twice

sed 's/add fast pkg boots-.*/add yinst pkg boots-5.0/g
s/add fast package boots-.*/add yinst pkg boots-5.0/g'

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

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