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

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

问题描述

我正在尝试替换配置文件中名为 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 yinst pkg boots-5.0

我尝试了以下 sed 命令:

I've tried the following sed commands:

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 正则表达式中的布尔或的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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