Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Elegant intersection()



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.



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