3 ms·
Someone explicitly called out and removed the Useless Use of cat but yet left the Useless Uses of grep, sort, and uniq in, I see. (-: * https://porkmail.org/e
by JdeBP 3y ago
Someone explicitly called out and removed the Useless Use of cat but yet left the Useless Uses of grep, sort, and uniq in, I see. (-:
* https://porkmail.org/era/unix/award#grep https://porkmail.org/era/unix/award#grep
awk '!/#/ { seen[$1]++; } END { for (k in seen) { print seen[k],k; } }' steves_hosts
- tedivm 3y agoI'd rather have the readability of the first command than the "efficiency" of your example.
- miles 3y agoGuilty as charged!
- arp242 3y agoI love awk as much as the next person and I've written some pretty large scripts with it, but the original grep/sort/uniq is a lot easier to understand in two seconds. I don't think it's "useless" at all. Also your awk isn't exactly identical as it's not sorted, but in this case that probably doesn't matter.
- j0hnyl 3y agoSame, I think I use grep | sort | uniq almost on the daily.
- dredmorbius 3y agoOne of the advantage of the array[item]++ / for (item in array) construct is that it avoids possibly expensive sorts (at the cost of generating potentially large arrays), and tends to be quite fast. 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.