Sorting output of AWK array

I need help to sort the output of an awk array

Example data

data="1 blue
2 green
3 blue
4 yellow
5 blue
6 red
7 yellow
8 red
9 yellow
10 yellow
11 green
12 orange
13 black"

My awk line to get output in one line

echo "$data" | awk  ' {arr[$2]++; next} END { for (i in arr) { if(arr>1 ) printf "%s(%s) ",i,arr};print "" }'

gives this output:

red(2) yellow(4) green(2) blue(3)

I need to count all hits, then print all with 2 or more hits sorted in one line like this (high to low):

yellow(4) blue(3) red(2) green(2)

I did look at the xargs and sort command, but this sorts on name.

echo "$data" | awk  ' {arr[$2]++; next} END { for (i in arr) { if(arr>1 ) printf "%s(%s) ",i,arr};print "" }' | xargs -n 1| sort | xargs

This gives :slight_smile:

blue(3) green(2) red(2) yellow(4)

Would be nice if all can be done in awk

Hi

echo "$data" | awk  ' {arr[$2]++; next} END { for (i in arr) { if(arr>1 ) printf "%s %s \n",i,arr};}' | sort -k2nr,2 | awk '$2="("$2")"' OFS="" | xargs
yellow(4) blue(3) green(2) red(2)

Guru.

It can, with GNU awk >= 4:

% printf "%s\n" "1 blue
2 green
3 blue
4 yellow
5 blue
6 red
7 yellow
8 red
9 yellow
10 yellow
11 green
12 orange
13 black" |
  awk 'END {
  PROCINFO["sorted_in"] = "@val_num_desc"
  for (d in data)
    if (data[d] > 1)
      printf "%s(%d) ", d, data[d]
  print x
  }
{
  data[$2] = ++count[$2]
  }'
yellow(4) blue(3) red(2) green(2)