Wednesday, May 13, 2009

A fun shell idiom.

To find what's in filea that isn't in fileb, I have always done

cat filea fileb fileb | sort | uniq -u
similar things give you union, intersection, symetric difference etc. Recently I shortened it to
cat filea fileb{,} | sort | uniq -u
which I thought was good fun.

5 comments:

Rob Ewaschuk said...

Neat. But surely in this contrived example, you're better off with:
cat file{a,b,b} | sort | uniq -u

Fergal Daly said...

Yeah, but it's only in my fake example that the 2 files share a prefix/suffix. E.g. "done_so_far" and "all_to_do".

I really didn't want to make a new blog but Blogger insisted said...

What's wrong with "comm -23 <(sort <filea) <(sort <fileb)"?

Rob Ewaschuk said...

it's 3 characters longer, that's what's wrong. =P

Fergal Daly said...

Adrian, I didn't know about comm or maybe I did but it's not in my common toolkit.