linux sort 排序_如何在Linux上使用sort命令按十六进制数字对文件排序?

linux sort 排序

The sort command has a -n option to sort a file by numbers. However, it does not work with hexadecimal numbers.

sort 命令具有-n选项,用于按数字对文件进行排序。 但是,它不适用于十六进制数。

For example, this file:

例如,此文件:

400000000	__crt0
400000039	__newr0
400001B14	get_my_task_id
400001C14	get_new_task_id
400001582	input_char
40000166E	input_q
400001A5D	input_q_exit
400002002	main
4000000DB	output_char
400001134	output_char_str
40000100C	output_id
40000018F	output_q
400000614	output_q_digits
400000B7E	output_q_hex
400000D3E	output_q_hex_j1
400000E3D	output_q_hex_j2
4000002FB	output_q_j1
400000444	output_q_j2
40000131F	output_str
400001385	output_str_j1
200000020	reg1
200000028	reg2
200000030	reg3
200000038	reg4
20400001000	status
400001E0C	t1
400001E70	t2
400001D14	task_id_to_ec_range

will be sorted to:

将被分类为:

400000B7E	output_q_hex
400000D3E	output_q_hex_j1
400000E3D	output_q_hex_j2
400001A5D	input_q_exit
400001B14	get_my_task_id
400001C14	get_new_task_id
400001D14	task_id_to_ec_range
400001E0C	t1
400001E70	t2
4000000DB	output_char
4000002FB	output_q_j1
40000018F	output_q
40000100C	output_id
40000131F	output_str
40000166E	input_q
200000020	reg1
200000028	reg2
200000030	reg3
200000038	reg4
400000000	__crt0
400000039	__newr0
400000444	output_q_j2
400000614	output_q_digits
400001134	output_char_str
400001385	output_str_j1
400001582	input_char
400002002	main
20400001000	status

But 200000020 is smaller than 40000166E.

但是200000020小于40000166E

How to sort a file by hexadecimal numbers on Linux using sort command?

如何在Linux上使用sort命令按十六进制数字对文件排序?

The combination of sort with perl, paste and cut produce the correct results (assume the file is file.txt).

perl sortpastecut结合在一起可以产生正确的结果(假设文件为file.txt)。

perl -lpe '$_=hex' file.txt | 
paste -d" " - file.txt  | 
sort -n | 
cut -d" " -f 2-

The first line will convert the hexadecimal numbers to decimal numbers. The second line concatenate the converted decimal numbers with the original text by row. Then sort them and cut the first decimal number fields away.

第一行会将十六进制数字转换为十进制数字。 第二行按行将转换后的十进制数字与原始文本连接在一起。 然后对它们进行排序,并删除第一个十进制数字字段。

200000020	reg1
200000028	reg2
200000030	reg3
200000038	reg4
400000000	__crt0
400000039	__newr0
4000000DB	output_char
40000018F	output_q
4000002FB	output_q_j1
400000444	output_q_j2
400000614	output_q_digits
400000B7E	output_q_hex
400000D3E	output_q_hex_j1
400000E3D	output_q_hex_j2
40000100C	output_id
400001134	output_char_str
40000131F	output_str
400001385	output_str_j1
400001582	input_char
40000166E	input_q
400001A5D	input_q_exit
400001B14	get_my_task_id
400001C14	get_new_task_id
400001D14	task_id_to_ec_range
400001E0C	t1
400001E70	t2
400002002	main
20400001000	status
Answered by Eric Z Ma.
埃里克·马(Eric Z Ma)回答。

翻译自: https://www.systutorials.com/how-to-sort-a-file-by-hexadecimal-numbers-on-linux-using-sort-command/

linux sort 排序

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值