Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shell Script - comparing lines of text, deleting matches

I've done some searching for this but cannot find what I'm after, specifically.

I have two files: "a.txt", "b.txt".

Each contains a list of email addresses, separated by newlines.

For all lines in "a.txt", I need to check for a match anywhere in "b.txt". If so, the email address in "a.txt" needs to be removed.

(Alternatively, a new file "c.txt" could be created with the output if that is easier.)

I'm using Mac OS X, so am looking for a shell script that could help, or pointers to how I'd go about constructing the script. Thanks for any help.

like image 978
SirRatty Avatar asked Apr 23 '10 03:04

SirRatty


2 Answers

grep -F -x -f b.txt -v a.txt > c.txt

or, equivalently,

fgrep -x -f b.txt -v a.txt
like image 66
Alex Martelli Avatar answered Nov 09 '22 14:11

Alex Martelli


Use comm command like this:

cat a.txt | sort > a2.txt
cat b.txt | sort > b2.txt
comm -23 a2.txt b2.txt > c.txt
like image 2
DVK Avatar answered Nov 09 '22 15:11

DVK