*X-seq*: zsh-users 13707*From*: Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx>*To*: zsh-users@xxxxxxxxxx*Subject*: Re: Elegant intersection()*Date*: Tue, 13 Jan 2009 01:04:12 -0800*In-reply-to*: <e5d50bd20901081650y2cde66c5r591252d081c15996@xxxxxxxxxxxxxx>*Mailing-list*: contact zsh-users-help@xxxxxxxxxx; run by ezmlm*References*: <e5d50bd20901081650y2cde66c5r591252d081c15996@xxxxxxxxxxxxxx>

On Jan 8, 4:50pm, Gerald Lai wrote: } } To print out lines that are common to 2 files, we can define a function to } find the intersection of the lines: } } function intersection {sort <(sort -u "$1") <(sort -u "$2") | uniq -d} So far so good, but ... } To do it for 3 files: } } function intersection {sort <(sort -u "$1") <(sort -u "$2") <(sort -u } "$3") | uniq -d} ... that doesn't really work, does it? That finds any line that is in at least 2 of the 3 files, not lines that are in all three. To get the 3-way intersection, you have to intersect the third file with the intersection of the first two, or replace uniq -d with uniq -c | grep "^ *$#"$'\t*' } Is there an elegant way to generalize this for "$@" in zsh? I guess I'd do something like intersection() { case $# in (0) true;; (2) sort -u "$1"; sort -u "$2";; (*) sort -u "$1"; shift; intersection "$@";; esac | sort | uniq -d } That's not really zsh-specific, though.

**References**:**Elegant intersection()***From:*Gerald Lai

Messages sorted by: Reverse Date, Date, Thread, Author