Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
865 views
in Technique[技术] by (71.8m points)

bash - replace multiple strings in one line with 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"

I tried

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

but that doesn't work.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...