HELP on 'sectional' or 'group' sorting

Hi,

I need to do some section/group sorting. At the moment, I get around it by grep group by group and then re-directing to the same 'final' output file.

Below is what's been sorted so far based on the second field

$ awk -F"," '{ { print $5 " == " $6 } }' /tmp/x.csv | grep -v "^env" | grep -v "^ " | grep -v "^$" | awk -F"." '{ print $1 }' | sort | uniq -c | sort -k2
     22 DEVELOPMENT == 10
     54 DEVELOPMENT == 11
     10 DEVELOPMENT == 12
     11 DEVELOPMENT == 8
      4 DEVELOPMENT == 9
     43 PRODUCTION == 10
     87 PRODUCTION == 11
     15 PRODUCTION == 12
     14 PRODUCTION == 8
      5 PRODUCTION == 9
     47 QUAL == 10
     92 QUAL == 11
     16 QUAL == 12
     11 QUAL == 8
      5 QUAL == 9
     35 TEST == 10
     96 TEST == 11
     15 TEST == 12
     15 TEST == 8
      4 TEST == 9

I want to sort the above so that the output will be as below. Let's refer to this as the final output. final.txt file.

     10 DEVELOPMENT == 12
     54 DEVELOPMENT == 11
     22 DEVELOPMENT == 10
      4 DEVELOPMENT == 9
     11 DEVELOPMENT == 8
     15 PRODUCTION == 12
     87 PRODUCTION == 11
     43 PRODUCTION == 10
      5 PRODUCTION == 9
     14 PRODUCTION == 8
     16 QUAL == 12
     92 QUAL == 11
     47 QUAL == 10
      5 QUAL == 9
     11 QUAL == 8
     15 TEST == 12
     96 TEST == 11
     35 TEST == 10
      4 TEST == 9
     15 TEST == 8

Because I don't know how to produce the final output using awk or sort. What I did instead is run several of the commands below, grepping for the string DEVELOPMENT, PRODUCTION and so on

$ awk -F"," '{ { print $5 " == " $6 } }' /tmp/x.csv | grep -v "^env" | grep -v "^ " | grep -v "^$" | awk -F"." '{ print $1 }' | sort 
| uniq -c | sort -k2 | grep "DEVELOPMENT" >> final.txt

Perhaps someone know a trick or too on how to do it a lot better. I don't see how using sort only will be able to do this. Advise much appreciated. Thanks.

For the sort: try:

sort -k2,2 -k4,4rn

Please be aware that most if not all of that awk ing, grep ing, uniq ing can be doen in one single awk only, and some awk version even offer a sort functionality.

Should you post a representative input and a desired output sample, people in here might come up with a solution taylored to your problem.