4 ms·
True, a bag of words would perform better. man bash | tr '[:upper:] ' '[:lower:]\n' | awk '/./ { bag[$1]++ } END { for(word in bag) { print bag[word], word } }
by kephra 13y ago
True, a bag of words would perform better.
man bash | tr '[:upper:] ' '[:lower:]\n' | awk '/./ { bag[$1]++ } END { for(word in bag) { print bag[word], word } }' | sort -rn | awk '{ print } NR>=10 { exit(0) }'
But it would require more typing and thinking.
sure one could do this in awk completely,
man bash | awk '
/./ {
for (i = 1; i<=NF; i++) {
bag[tolower($i)]++
}
}
END {
for (i = 1; i<=10; i++) {
score=0;
for(word in bag) {
if (bag[word] > score) {
score=bag[word];
best=word
}
}
printf "%s ", best
delete bag[best]
}
printf "\n"
}'
if the requirement is: please chose one language and not the complete Unix babylon.