简体   繁体   English

Unix/Linux:在逗号后按数字排序

[英]Unix/Linux: Sorting by numbers after a comma

I am not very experienced with Linux/Unix, and I have a data file, with each line following the syntax of我对 Linux/Unix 不是很有经验,我有一个数据文件,每一行都遵循以下语法

[name of a crime that was committed],[numberOfOccurences]

For example, here are some of the lines of the file:例如,这里是文件的一些行:

Partial 311,2
Carjacking Att,1
Rtr,1
Loiters,4
459 Susp Poss,2
Boat,6
Dog Attack,10

I want to sort the file in descending order by the numbers that follow the comma.我想按逗号后面的数字按降序对文件进行排序。 I do not know the number of digits that follow the comma.我不知道逗号后面的位数。 I know I will use the "sort" command, with -r for descending order, but can somebody tell me the exact command to use?我知道我会使用“排序”命令,-r 表示降序,但有人能告诉我要使用的确切命令吗?

Thanks in advance!提前致谢!

To sort in descending order: sort -t, -k2,2rn input-file按降序排序: sort -t, -k2,2rn input-file

The -t, tells sort that fields are separated by commas. -t,告诉 sort 字段用逗号分隔。 The -k2,2 tell sort to sort starting at field 2 and ending at field 2. The -r reverses the order for that sort key, and the -n tells sort that the field is to be ordered numerically (as opposed to lexicographically) -k2,2告诉 sort 从字段 2 开始排序并在字段 2 结束。 -r反转该排序键的顺序, -n告诉 sort 该字段将按数字排序(而不是按字典顺序)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM