Unix command to find string set intersections or outliers? Unix command to find string set intersections or outliers? unix unix

Unix command to find string set intersections or outliers?


It appears that grep -L solves the real problem of the poster, but for the actual question asked, finding the intersection of two sets of strings, you might want to look into the "comm" command. For example, if file1 and file2 each contain a sorted list of words, one word per line, then

$ comm -12 file1 file2

will produce the words common to both files. More generally, given sorted input files file1 and file2, the command

$ comm file1 file2

produces three columns of output

  1. lines only in file1
  2. lines only in file2
  3. lines in both file1 and file2

You can suppress the column N in the output with the -N option. So, the command above, comm -12 file1 file2, suppresses columns 1 and 2, leaving only the words common to both files.


Intersect:

# sort file1 file2 | uniq -ddad

Left unique:

# sort file1 file2 | uniq -ubobmom


From http://www.commandlinefu.com/commands/view/5710/intersection-between-two-files:

Intersection between two (unsorted) files:

grep -Fx -f file1 file2

Lines in file2 that are not in file1:

grep -Fxv -f file1 file2

Explanation:

  • The -f option tells grep to read the patterns to look for from a file. That means that it performs a search of file2 for each line in file1.
  • The -F option tells grep to see the search terms as fixed strings, and not as patterns, so that a.c will only match a.c and not abc,
  • The -x option tells grep to do whole line searches, so that "foo" in file1 won't match "foobar" in file2.
  • By default, grep will show only the matching lines, giving you the intersection. The -v option tells grep to only show non-matching lines, giving you the lines that are unique to file2.