tags:

views:

649

answers:

4

Is there a UNIX command on par with

sort | uniq

to find string set intersections or "outliers".

An example application: I have a list of html templates, some of them have {% load i18n %} string inside, others don't. I want to know which files don't.

edit: grep -L solves above problem.

How about this:

file1:

mom
dad
bob

file2:

dad

%intersect file1 file2

dad

%left-unique file1 file2

mom
bob
+5  A: 

Maybe I'm misunderstanding the question, but why not just use grep to look for the string (use the -L option to have it print the names of files that don't have the string in them).

In other words

grep -L "{% load i18n %}" file1 file2 file3 ... etc

or with wildcards for the file names as appropriate.

Tyler McHenry
thanks, man! it works.
Evgeny
For faster searching, I'd use -F too, since it's just a fixed string.
Chris Jester-Young
what about set intersections?
Evgeny
not sure what you mean by that in this context
Tyler McHenry
+2  A: 

from man grep

-L, --files-without-match

Suppress normal output; instead print the name of each input file from which no output would normally have been printed. The scanning will stop on the first match.

So if your templates are .html files you want:

grep -L '{% load i18n %}' *.html
artlung
+3  A: 

Intersect:

# sort file1 file2 | uniq -d
dad

Left unique:

# sort file1 file2 | uniq -u
bob
mom
John Kugelman
+3  A: 

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.

Dale Hagglund
thanks. you have a typo in the first example. never heard of this command.
Evgeny
I took the liberty of fixing it
Nathan Fellman
Thanks for the edit.
Dale Hagglund
Don't forget to run your files through sort before the comparison. I did, and the results sent me on a wild goose chase.
I. J. Kennedy
I do mention above that each file contains a "sorted list of words", but it might not jump right out at you.
Dale Hagglund