You keep giving us examples of what you want without ever specifying the algorithm that should be used to determine which input rows should be written to the output and which input rows should be discarded. Despite repeatedly saying that you want to use the sort utility, one common thing in all of your sample desired outputs is that none of the output you say you want is sorted.
If you're willing to use awk instead of sort and your unstated algorithm is something like:
For each set of lines where the 1st three fields are identical:
- choose any single line from that set that has the largest number of fields and print it
- do not print any other line in that set.
then the following may do what you want:
awk '
lfc[$1, $2, $3] < NF {
lfc[$1, $2, $3] = NF
l[$1, $2, $3] = $0
}
END { for(k in l)
print l[k]
}' file
If you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk , /usr/xpg6/bin/awk , or nawk .
If file contains (as in your 1st example):
A: Apple 2 B:Bolls 4 total_count = 6
A: pens 4 B:Bags 4
A: pens 4 B:Bags 4 total_count = 8
A: pens 4 B:Bags 4
A: pens 4 B:Bags 4 total_count = 8
A: cells 6
A: jobs 6
the output produced is:
A: jobs 6
A: cells 6
A: Apple 2 B:Bolls 4 total_count = 6
A: pens 4 B:Bags 4 total_count = 8
If file contains (as in your 2nd example):
A: Apple 2 B:Bolls 4 total_count = 6
A: pens 4 B:Bags 4
A: pens 4 B:Bags 4 total count = 8
A: cells 6
A: jobs 6
the output produced is:
A: jobs 6
A: cells 6
A: Apple 2 B:Bolls 4 total_count = 6
A: pens 4 B:Bags 4 total count = 8
And, if file contains (as in your 3rd example):
A: pens 4
A: pens 4 B:Bags 4 total_count = 8
A: cells 6
A: jobs 6
the output produced is:
A: jobs 6
A: cells 6
A: pens 4 B:Bags 4 total_count = 8
Is this what you're trying to do?