问题
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.
回答1:
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.
回答2:
You could use an awk script to create a file that left-pads the strings to a fixed length, sort the resulting file, and then strip leading spaces on the result.
It's been a while since I used awk, but as I recall it wasn't difficult to output things right-justified.
回答3:
Try sort -n filename
. The -n
flag as per the man page "compare according to string numerical value".
Update: as pointed out by @barlop, the -n
option does not work for hex numbers.
来源:https://stackoverflow.com/questions/5020656/sort-hex-numbers-of-different-length-from-the-command-line