简体   繁体   中英

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

[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?

Thanks in advance!

To sort in descending order: sort -t, -k2,2rn input-file

The -t, tells sort that fields are separated by commas. 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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