[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: How to sort and count efficiently?
From: |
Assaf Gordon |
Subject: |
Re: How to sort and count efficiently? |
Date: |
Sun, 30 Jun 2019 10:52:30 -0600 |
User-agent: |
Mutt/1.11.4 (2019-03-13) |
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