Comparing two lists with a shell script
Suppose I have two lists of numbers in files f1, f2, each number one per line. I want to see how many numbers in the first list are not in the second and vice versa. I am currently using grep -f f2 -v f1 and then iterating over this with a shell script. It's pretty slow (quadratic time hurts). Is there a better way to do this?
+2
source to share
3 answers
In the special case, when one file is a subset of another, the following:
cat f1 f2 | sort | uniq -u
will only list lines in the larger file. And, of course, the pipeline wc -l
will show the bill.
However, this is not exactly what you described.
This one-liner often serves my special needs, but I'd like to see a more general solution.
+1
source to share