Search code examples
windowslinuxcommand-linecygwingnuwin32

sort hex numbers of different length from the command line?


If I have a file of hex numbers of different length e.g.

1F
b
c

How can I sort them from the command line?

Linux solutions are welcome, though I'll be using windows and cygwin or gnuwin32.

Note: I clearly can't use SORT 'cos that will keep them in that order which is wrong.


Solution

  • cat thefile | while read line; do printf "%d %s\n" "0x$line" "$line"; done | sort -n | awk '{print $2}'
    

    This retains the original upper/lower case of the hexadecimal numbers.