Correcting myself:

On Sun, Jun 30, 2019 at 10:08:46AM -0600, Assaf Gordon wrote:
> On Sun, Jun 30, 2019 at 07:34:19AM -0500, Peng Yu wrote:
> > 
> > I have a long list of string (each string is in a line). I need to
> > count the number of appearance for each string.
> > 
> > [...] Does anybody know any better way
> > to make the sort and count run more efficiently?
> > 
> 
> Or using gnu awk:

use 'asorti' instead of 'asort', with the two-parameter variant:


  $ printf "%s\n" a c b b b b b b c \
        | awk 'a[$1]++ {}
               END { n = asorti(a,b)
                     for (i = 1; i <= n; i++) {
                        print b[i], a[b[i]]
                     }
                   }'
  a 1
  b 6
  c 2


For more details see:
https://www.gnu.org/software/gawk/manual/html_node/Array-Sorting-Functions.html#Array-Sorting-Functions

-assaf


Reply via email to