Reputation: 13128
I want to use sed to do this. I have 2 files:
keys.txt:
host1
host2
test.txt
host1 abc
host2 cdf
host3 abaasdf
I want to use sed to remove any lines in test.txt that contains the keyword in keys.txt. So the result of test.txt should be
host3 abaasdf
Can somebody show me how to do that with sed?
Thanks,
Upvotes: 2
Views: 301
Reputation: 58430
This might work for you (GNU sed):
sed 's|.*|/^&\\>/d|' keys.txt | sed -f - test.txt
Upvotes: 0
Reputation: 246817
fgrep -v -f
is the best solution. Here are a couple of alternatives:
A combination of comm
and join
comm -13 <(join keys.txt test.txt) test.txt
or awk
awk 'NR==FNR {key[$1]; next} $1 in key {next} 1' keys.txt test.txt
Upvotes: 2
Reputation: 51613
I'd recommend using grep
for this (especially fgrep
since there are no regexps involved), so
fgrep -v -f keys.txt test.txt
does it fine. With sed
quickly this works:
sed -i.ORIGKEYS.txt ^-e 's_^_/_' -e 's_$_/d_' keys.txt
sed -f keys.txt test.txt
(This modifies the original keys.txt
in place - with backup - to a sourceable sed script.)
Upvotes: 2