Rob
Rob

Reputation: 33

sed delete string matching a variable value containing slashes

I'm trying to use sed to clean a list. Any lines appearing in $salvation should be removed from the already set $naughtyList.

I have;

$ echo "$naughtyList"
ONE/1/one
TWO/2/two
THREE/3/three
FOUR/4/four
FIVE/5/five

and;

$ echo "$salvation"
TWO/2/two
FOUR/4/four

The problem is the slash characters. I can't escape them when trying;

for line in $(echo $salvation); do
naughtyList=$(echo $naughtyList | sed "/$line/d")
done
sed: -e expression #1, char 6: unknown command: `2'
sed: -e expression #1, char 7: unknown command: `4'

Is there a way to pass $line, or do I have to modify the input patterns?

Thanks in advance.

Upvotes: 1

Views: 53

Answers (1)

anubhava
anubhava

Reputation: 785196

Better to use grep -f here with process substitution:

grep -vFf <(echo "$salvation") <(echo "$naughtyList")
ONE/1/one
THREE/3/three
FIVE/5/five

To update naughtyList variable, use:

naughtyList=$(grep -vFf <(echo "$salvation") <(echo "$naughtyList"))

Upvotes: 1

Related Questions