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

Possible Duplicate:
Linux tools to treat files as sets and perform set operations on them

I have two data sets, A and B. The format for each data set is one number per line. For instance,


Some of the data in A are not included in data set B. How to list all of these data in A, and how to list all of those data shared by A and B. How can I do that using Linux/UNIX commands?

share|improve this question

marked as duplicate by Gilles, Michael Mrozek Jan 15 '12 at 18:31

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

up vote 10 down vote accepted

Use the comm command.

If you lists are in files listA and listB:

comm listA listB

By default, comm will return 3 columns. Items only in listA, items only in listB, and items common to both lists.

You can suppress individual columns, with a -1, -2, or -3 arg.

share|improve this answer
The answer assumes listA and listB are already sorted. A more general solution: comm <(sort listA) <(sort listB) – HongboZhu Sep 19 '14 at 7:33
Very simple solution. Is the comm command deployed in all linux distro? – ruffp Jul 6 '15 at 8:07

This will give you the unique items that exist in A but not in B:

cat A|perl -ne '$z=$_;chomp($z);$y=`grep $z B`;if ($y== "") {print "\n$z";}'|sort -u

This will give you the list of common items in both A and B:

cat A |xargs -i grep {} B|sort -u
share|improve this answer

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