且构网

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

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

更新时间:2023-02-12 19:18:59

sed正则表达式中,您必须转义(|).

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/\b\($dellist\)\b//g" benoietigte_packete.list

我添加了\b,因此它只匹配整个单词.

I've added the \b 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.