A Telecomputer Scientist's Rhymebook

Set Operations on the Command Line

May 19, 2009 · 1 Comment

Performing set operations, such as union, intersection, and difference, is easy in linux using basic command line utilities like sort and comm (for compare).

Here is a cheat sheet for the basic set operations:

Set Operation Linux Equivalent
A UNION B
Union
sort -mu A B
A INTERSECTION B
Set Intersection
sort A B | uniq -d

-or-

comm -12 A B*

A MINUS B
Set Difference
comm -23 A B*

* Both A and B need already be sorted.

Here is a more in depth article with some comments on performance and some alternative commands.

Categories: Hairy Banana Computer Science
Tagged: , , , , , , , , ,

1 response so far ↓

Leave a Comment