简体   繁体   中英

Uniq -c sort by column?

Can uniq -c only get the number of occurrences of column 2 and then sort it according the number of occurrence? Also, after sorting, I don't want to show the number of occurrences. How to do it?

Here is my example input:

111111111 40
222222222 50
333333333 60
111111111 40
222222222 60 
333333333 50

To Be:

222222222 50
222222222 60
333333333 50 
333333333 60
111111111 40
$ cat file 
111111111 40
222222222 50
333333333 60
111111111 40
222222222 60 
333333333 50
$ sort file | uniq -c | sort -n -t ' ' -k 1 | awk '{print $2" "$3}'
222222222 50
222222222 60
333333333 50
333333333 60
111111111 40
$
  1. sort file | uniq -c sort file | uniq -c gives you 3 columns in order: num(occurences) col1 col2
  2. then sort -n -t ' ' -k 1 sorts the piped input (numerically -n according to num(occurences)) that is delimited by space(specified by t switch) according to 2nd column/key( -k 2 )
  3. then awk '{print $2" "$3}' just prints the 2nd and 3rd field removing the number of occurrences field.

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