procmail
[Top] [All Lists]

Re: compare files

1998-10-13 10:11:27
1998-10-13-13:00:12 Jost Schaper:
I have a crontab-file make send me the result of a 'who closed-list'
every day.
I would like to compare the new 'who closed-list' with another
majordomo-list and add only the new users of the 'who closed-list' to
the other majordomo-list.

When I want to find who is on one list and not on another at the Unix shell
level, I use comm(1). If the two lists are already sorted, then it's simply

        comm -23 one-list another-list | wherever you want

comm(1) generates a 3-column output, given two sorted files. The first column
contains lines found only in the first file, the second column contains lines
found only in the second, and the third column contains lines found in both.
comm(1) takes arguments -1, -2, and -3 (which can be combined, as above) to
_suppress_ the named columns; hence "comm -23" produces a single column output
of lines found only in the first file. It only works if both files are sorted.
If they aren't, then you can either use tmp files; e.g.

        #!/bin/sh
        tmp=/tmp/`basename $0`.$$
        trap "rm -f $tmp.1 $tmp.2" 0
        sort <$1 >$tmp.1
        sort <$2 >$tmp.2
        comm -23 $tmp.1 $tmp.2

or else, if you are using a nice modern POSIX shell like bash, you can set up
the pipelines using named pipes, with e.g.

        comm -23 <(sort one-list) <(sort another-list) | ...

How to best to glue this into procmail I don't know, it would depend on what
you wish to accomplish. Often the answer to the question "how to I do this
ornate Unix operation from procmail" is "with a helper script".

-Bennett

<Prev in Thread] Current Thread [Next in Thread>