Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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:

setA:
2
4
5
6

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
add comment

2 Answers 2

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
add comment

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
add comment

Your Answer

 
discard

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.