Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am looking for an efficient way to achieve set difference from 2 files. I do know that grep -vxF -f setA setB gives me B - A. This is what I want, but my files are as follows:


set B:
1 123
2 abc
3 def
4 hij

I want the set difference to be calculated based on the first column of set B; hence the expected output for B - A would be:

1 123
3 def

Any pointers on how to achieve this?

share|improve this question
up vote 4 down vote accepted
awk 'NR==FNR {key[$1]; next} !($1 in key)' setA setB
1 123
3 def

That reads the first file and stores the "ids" in the array "key", then it only prints from the 2nd file if the id is not in the key array.

share|improve this answer

That's typically what join is for:

join -v2 a b

That's joining the files on the first column (which is assumed to be sorted lexicographically), and print the rows of the second file which are not paired.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.