by miles · 3y ago · 0 comments
grep -v '#' steves_hosts | awk '{print $1}'| sort | uniq -c
[1] https://news.ycombinator.com/item?id=11456562 find . -name hosts -exec grep -v '^#' {} \; | awk '{print $1}'| sort | uniq -c
43029
204 #
4289934 0.0.0.0
10763 127.0.0.1
18 255.255.255.255
54 ::1
1 analytics.shein.co.uk
1 analytics.shein.com
1 auxilium.ftb.team
1 bstats.org
1 fe00::0
17 fe80::1%lo0
33 ff00::0
17 ff02::1
17 ff02::2
17 ff02::3
1 mcmc.dev
1 mcstats.org
1 metrics.shmeeb.net
1 openeye.openmods.info
And looking at those last ones, looks like some files have a different format. For example, the data/minecraft-hosts/hosts file, ~/D/P/hosts master grep -irn 'openeye.openmods.info' .
./alternates/fakenews/hosts:174307:0.0.0.0 openeye.openmods.info
...
./hosts:174306:0.0.0.0 openeye.openmods.info
./data/minecraft-hosts/hosts:9:openeye.openmods.info
~/D/P/hosts master grep -irn 'metrics.shmeeb.net' .
...
./data/minecraft-hosts/hosts:8:metrics.shmeeb.net* https://porkmail.org/era/unix/award#grep
awk '!/#/ { seen[$1]++; } END { for (k in seen) { print seen[k],k; } }' steves_hostsAlso your awk isn't exactly identical as it's not sorted, but in this case that probably doesn't matter.
That's even if you end up sorting the output by frequency, as the summary listing is typically shorter than the overall input data:
{a[$1]++}
END {for (i in array) printf("%6i %s\n", a[i], i) | "sort -k1nr | cat -n";}
The accumulator/loop idiom is ... fairly readily recognisable to someone familiar with awk. 190625 0.0.0.0
3 127.0.0.1
1 255.255.255.255
3 ::1
1 fe80::1%lo0
2 ff00::0
1 ff02::1
1 ff02::2
1 ff02::3