用sed替换一行中的多个字符串 [英] replace multiple strings in one line with sed

查看:645
本文介绍了用sed替换一行中的多个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个软件包名称列表,想删除一些带有sed

I have a list of packagenames and want to delete some of those with sed

echo "package1 package2 package24 package44 package66 package12345" > benoetigte_pakete.list

如何删除另一个列表中的某些内容?

How can I delete some of those that are in another list?

dellist="package24|package66"

我尝试了

cat benoetigte_pakete.list | sed "s/(package24|package66)//"

但这是行不通的.

推荐答案

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.

这篇关于用sed替换一行中的多个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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