[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [sort] Suggestion: count keys from the end of record
From: |
Rob Landley |
Subject: |
Re: [sort] Suggestion: count keys from the end of record |
Date: |
Mon, 14 Aug 2023 16:38:09 -0500 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.10.0 |
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...)
Rob