f

Set Operations in the Unix Shell

```http://www.catonmat.net/blog/set-operations-in-unix-shell/

Quoting from the URL above:

A while ago I wrote about how I solved the Google Treasure Hunt Puzzle
Nr. 4 about prime numbers. I took an unusual approach and solved this
problem entirely from the Unix shell. The solution involved finding the
intersection between a bunch of files containing numbers. This lead me
to an idea to write a post about how to do various set operations from
the shell by using common utilities such as sort, uniq, diff, grep,
head, tail, comm, and others.

I'll cover the following set operations in this article:

Set Membership. Test if an element belongs to a set. Set Equality. Test
if two sets contain the same elements. Set Cardinality. Return the
number of elements in the set. Subset Test. Test if a given set is a
subset of another set. Set Union. Find union of two sets. Set
Intersection. Find intersection of two sets. Set Complement. Given two
sets A and B, find all elements in A that are not in B. Set Symmetric
Difference. Find symmetric difference of two sets. Power Set. Generate
all subsets of a set. Set Cartesian Product. Find A x B. Disjoint Set
Test. Test if two sets are disjoint. Empty Set Test. Test if a given set
is empty. Minimum. Find the smallest element of a set. Maximum. Find the
largest element of a set.

...
```
 0
Rich
12/24/2016 3:55:07 PM
comp.misc 2506 articles. 1 followers.

6 Replies
719 Views

Similar Articles

[PageSpeed] 4

```>    This lead me to an idea to write a post about how to do various set
>    operations from the shell by using common utilities such as sort,
>    uniq, diff, grep, head, tail, comm, and others.

Unix shell programming is hopelessly broken and should not be promoted.

I say that with a heavy heart.

Marko
```
 0
Marko
12/24/2016 7:16:39 PM
```On Sat, 24 Dec 2016 21:16:39 +0200, Marko Rauhamaa wrote:

>>    This lead me to an idea to write a post about how to do various set
>>    operations from the shell by using common utilities such as sort,
>>    uniq, diff, grep, head, tail, comm, and others.
>
> Unix shell programming is hopelessly broken and should not be promoted.

I use it for simple things. After that, I use REXX.

--
Using UNIX since v6 (1975)...

Use the BIG mirror service in the UK:
http://www.mirrorservice.org
```
 0
Bob
12/24/2016 9:23:22 PM
```On 2016-12-24, Bob Eager <news0006@eager.cx> wrote:
> On Sat, 24 Dec 2016 21:16:39 +0200, Marko Rauhamaa wrote:
>
>>>    This lead me to an idea to write a post about how to do various set
>>>    operations from the shell by using common utilities such as sort,
>>>    uniq, diff, grep, head, tail, comm, and others.
>>
>> Unix shell programming is hopelessly broken and should not be promoted.
>
> I use it for simple things. After that, I use REXX.

s/REXX/perl/;

--
Today is Pungenday, the 66th day of The Aftermath in the YOLD 3182
I don't have an attitude problem.
If you have a problem with my attitude, that's your problem.
```
 0
Huge
12/24/2016 11:00:26 PM
```Huge <Huge@nowhere.much.invalid>:

> On 2016-12-24, Bob Eager <news0006@eager.cx> wrote:
>> On Sat, 24 Dec 2016 21:16:39 +0200, Marko Rauhamaa wrote:
>>
>>>>    This lead me to an idea to write a post about how to do various
>>>>    set operations from the shell by using common utilities such as
>>>>    sort, uniq, diff, grep, head, tail, comm, and others.
>>>
>>> Unix shell programming is hopelessly broken and should not be
>>> promoted.
>>
>> I use it for simple things. After that, I use REXX.
>
> s/REXX/perl/;

I think Python is currently the best choice.

Marko
```
 0
Marko
12/24/2016 11:03:19 PM
```On Sun, 25 Dec 2016 01:03:19 +0200, Marko Rauhamaa wrote:

> Huge <Huge@nowhere.much.invalid>:
>
>> On 2016-12-24, Bob Eager <news0006@eager.cx> wrote:
>>> On Sat, 24 Dec 2016 21:16:39 +0200, Marko Rauhamaa wrote:
>>>
>>>>>    This lead me to an idea to write a post about how to do various
>>>>>    set operations from the shell by using common utilities such as
>>>>>    sort, uniq, diff, grep, head, tail, comm, and others.
>>>>
>>>> Unix shell programming is hopelessly broken and should not be
>>>> promoted.
>>>
>>> I use it for simple things. After that, I use REXX.
>>
>> s/REXX/perl/;
>
> I think Python is currently the best choice.

The best choice is what you know, and are comfortable with.
--
Using UNIX since v6 (1975)...

Use the BIG mirror service in the UK:
http://www.mirrorservice.org
```
 0
Bob
12/25/2016 12:56:38 AM
```On 2016-12-25, Bob Eager <news0006@eager.cx> wrote:
> On Sun, 25 Dec 2016 01:03:19 +0200, Marko Rauhamaa wrote:
>
>> Huge <Huge@nowhere.much.invalid>:
>>
>>> On 2016-12-24, Bob Eager <news0006@eager.cx> wrote:
>>>> On Sat, 24 Dec 2016 21:16:39 +0200, Marko Rauhamaa wrote:
>>>>
>>>>>>    This lead me to an idea to write a post about how to do various
>>>>>>    set operations from the shell by using common utilities such as
>>>>>>    sort, uniq, diff, grep, head, tail, comm, and others.
>>>>>
>>>>> Unix shell programming is hopelessly broken and should not be
>>>>> promoted.
>>>>
>>>> I use it for simple things. After that, I use REXX.
>>>
>>> s/REXX/perl/;
>>
>> I think Python is currently the best choice.
>
> The best choice is what you know, and are comfortable with.

Hear, hear.

Dunno why, but with shell programming I have good days and bad days.
With my current alternative first choice of Python the experience is a
lot more consistent.

Happy Christmas.

--
"History does not repeat itself, but it does rhyme"    -- Mark Twain
```
 0
Paul
12/25/2016 1:59:09 AM