Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

replace multiple strings in one line with sed

Tags:

bash

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.

like image 264
rubo77 Avatar asked Dec 21 '25 23:12

rubo77


1 Answers

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

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

like image 125
Barmar Avatar answered Dec 23 '25 23:12

Barmar