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
blue(3) green(2) red(2) yellow(4)
Would be nice if all can be done in awk