且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

用 sed 替换一行中的多个字符串

更新时间:2023-02-12 20:47:54

sed 正则表达式中,你必须转义 (, |, and)代码>.

In sed regexps you have to escape (, |, and ).

您还需要使用 g 修饰符,以便它替换该行上的所有匹配项,而不仅仅是第一个匹配项.

You also need to use the g modifier so it replaces all matches on the line, not just the first match.

dellist="package24|package66"
# escape the pipes
dellist=${dellist//|/\|}
sed "s/($dellist)//g" benoietigte_packete.list

我添加了 所以它只匹配整个单词.

I've added the so it only matches whole words.

根据您拥有的 sed 版本,您还可以使用 -E-r 选项来使用扩展的正则表达式.

Depending on the version of sed you have, you can also use the -E or -r options to use extended regular expressions.