Sample Header Ad - 728x90

sort by hex value

19 votes
5 answers
12529 views
Using coreutils sort, how can I sort numerically by a hexadecimal value (field)? I was expecting something along the lines of sort -k3,3x file_to_sort however, such an x does not exist. Edit: Best solution I came up with so far is: { echo ibase=16; cut -d' ' -f3 file_to_sort; } | bc | paste -d: - file_to_sort | sort -t: -k1,1n | cut -d: -f2- where the cut -d' ' -f3 isolates the search field (this is -k3,3 — this may vary, of course), and bc does conversion to decimal (requires upper-case hex, without 0x prefix, matching my case). Then I join, sort, and split columns. Minimal sample input: 5 hhf 25 3 ezh ae 1 hdh d12 2 ukr 9f 4 jjk 7 Expected output (file sorted by hex third column): 4 jjk 7 5 hhf 25 2 ukr 9f 3 ezh ae 1 hdh d12
Asked by stefan (1151 rep)
Jun 29, 2014, 02:32 PM
Last activity: Apr 27, 2024, 05:36 PM