用 sed 替换一行中的多个字符串 [英] replace multiple strings in one line with sed
本文介绍了用 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
正则表达式中,你必须转义 (
, |
, 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.
这篇关于用 sed 替换一行中的多个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文