user354134
user354134

Reputation:

Comm with alternate sort order

The Unix command "comm" will find common/unique lines in two files provided that the two files are sorted in lexical order.

Suppose I have two files sorted with "sort -nr". Is there a comm-like program that will work on these files?

Of course, I could always resort the files lexically, comm them, and then toss the lexically sorted versions, but that's inefficient.

It seems it would be easy to write a comm-like program that does this, so I'm guessing one exists?

Upvotes: 1

Views: 480

Answers (1)

Kristján
Kristján

Reputation: 18813

No such program exists. Sorting numerically is significantly different than sorting lexically, so it would be a big addition to comm itself. Given the Unix philosophy of composing small tools that do one thing well, and since this is already easily accomplishable with existing tools, I'd wager there was just never enough need to warrant writing a numeric version or option.

Given two files all and even containing the reverse-sorted numbers you'd expect, this does just the right thing:

comm -12 <(sort all) <(sort even) | sort -rn

Upvotes: 1

Related Questions