使用unix命令对文件进行排序

使用unix命令对文件进行排序

输入文件:

tmp-1064,address3,state,country
tmp-1061,address1,state,country
tmp-1060,address2,state,country
tmp-106,address4,state,country

当前输出文件

tmp-1060,address3,state,country
tmp-1061,address1,state,country
tmp-106,address2,state,country
tmp-1064,address4,state,country

所需的输出文件

tmp-106,address3,state,country
tmp-1060,address1,state,country
tmp-1061,address2,state,country
tmp-1064,address4,state,country

答案1

告知sort您的字段以逗号 ( ) 分隔-t,并使用第一个 (-k1) 字段进行排序,但仅查看该字段的第 5 个字符及之后的字符。添加n排序选项以按数字排序:

sort -t, -k1.5n input

使用 GNU sort,你可以看到它发生:

$ sort -t, -k1.5n --debug input
sort: using ‘en_US.UTF-8’ sorting rules
sort: key 1 is numeric and spans multiple fields
tmp-106,address4,state,country
    ___
______________________________
tmp-1060,address2,state,country
    ____
_______________________________
tmp-1061,address1,state,country
    ____
_______________________________
tmp-1064,address3,state,country
    ____
_______________________________

相关内容