coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [sort] Suggestion: count keys from the end of record


From: Pádraig Brady
Subject: Re: [sort] Suggestion: count keys from the end of record
Date: Tue, 15 Aug 2023 10:27:08 +0100
User-agent: Mozilla Thunderbird

On 14/08/2023 22:38, Rob Landley wrote:


On 8/14/23 01:56, Renaud Pacalet wrote:
On 13/08/2023 17:09, Pádraig Brady wrote:
On 13/08/2023 15:40, Renaud Pacalet wrote:
I am a bit surprised that it has never been suggested before but I could
not find any trace of it, so here it is:

Would it be possible to specify the sorting keys from the end of the
record, instead of the beginning. Something like sort -k-1,-1 to sort on
the last field of each record, sort -k-2,-2 for the second to last...

This would be handy when the format has a variable number of fields per
record but the last records are well defined.

I vaguely remember this being suggested before ...
https://lists.gnu.org/archive/html/bug-coreutils/2006-12/msg00008.html

Ah, so it has been suggested before, thanks for the link.

I got about halfway through implementing this in toybox because "eh, why not?"
and hit a design question:

In the existing code, a number off the _right_ edge doesn't count, ala:

   $ echo -e 'one\ntwo\nthree\nfour' | sort -sk2
   one
   two
   three
   four
   $ echo -e 'one\ntwo\nthree\nfour' | sort -sk1
   four
   one
   three
   two

So when a negative number is off the _left_ edge, does it get trimmed to field 1
or does it not match? Ala would -k-2 become -k1 or -k9999999 for lines that
don't have two fields?

(I assume the same answer applies to the dot-fields, ala -k-1.-3,-2.-5 and so 
on...)

I would expect no match, rather than clamping to the first field.
That would be most consistent.

cheers,
Pádraig




reply via email to

[Prev in Thread] Current Thread [Next in Thread]