coreutils
[Top][All Lists]
Advanced

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

Re: sort by hex numeric value


From: Assaf Gordon
Subject: Re: sort by hex numeric value
Date: Sat, 19 Aug 2017 08:26:31 -0600
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.2.1

Hello,

On 19/08/17 03:57 AM, Ciro Santilli wrote:
> Is it possible to sort by hex values, with or without the 0x prefix? If
> not, feature request!

Not currently possible with 'sort' alone, but easily done
if combined with awk:

> $ printf '0x0a\n0x1\n' | sort -n
> 

    $ printf '0a\n1\n' \
         | gawk '{ print $0, strtonum("0x" $1) }' \
         | sort -k2,2n | gawk '{NF--;print $0}'
    1
    0a

Note 'strtonum' is GNU gawk (gawk) function, not a standard awk
function (e.g. the default on Debian systems is mawk, gawk needs
to be installed specifically).

This pattern is referred to as "decorate-sort-undecorate".
"decorate" here means "add a column by converting the input
to something sort does recognize" and undecorate
means "remove the column after sorting".

See other similar past requests:
Sort with file extension:
 https://lists.gnu.org/archive/html/bug-coreutils/2017-05/msg00003.html

Sort by IP address:
 https://lists.gnu.org/archive/html/bug-coreutils/2015-06/msg00076.html
(this post also discusses the possibility of extending 'sort' to support
generic decoration internally.

Sort by line length:
 https://lists.gnu.org/archive/html/bug-coreutils/2009-07/msg00107.html


> Not sure what is the best approach about backwards compatibility.
> Maybe allow a base number after -n e.g.:
>  sort -n16
> or add a new option:
>  sort -H

As there are many different needs and requests to some by different
things, the approach will likely be similar to the one
discussed in the 'sort by IP address', above.
(that is, if sort(1) is to be modified at all).


regards,
 - assaf





reply via email to

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