Skip to main content
2 of 4
fixed "word" as per manatwork comment
Stéphane Chazelas
  • 584.6k
  • 96
  • 1.1k
  • 1.7k
file=myfile
for class in alnum alpha blank cntrl digit graph lower print punct space upper xdigit
do
  printf '%7s: %d\n' "$class" "$(tr -cd "[:${class}:]" < "$file" | wc -c)"
done

ascii and word are not standard character classes and are bash specific. word is alnum plus underscore, and ascii is characters 0 to 127, so you can do:

printf '%7s: %d\n' word "$(tr -cd "_[:alnum:]" < "$file" | wc -c)"
printf '%7s: %d\n' ascii "$(LC_ALL=C tr -cd '\0-\177' < "$file" | wc -c)"
Stéphane Chazelas
  • 584.6k
  • 96
  • 1.1k
  • 1.7k