logoalt Hacker News

lilli_putyesterday at 6:42 PM0 repliesview on HN

I think that you can elegantly implement the second problem in bash:

  grep -Eo '[A-Za-z]+' README.md \
      | tr A-Z a-z \
      | {
      distinct=()
      declare -A freqs
      while read word; do
          [[ -v freqs["$word"] ]] || distinct+=("$word")
          ((freqs["$word"]++))
      done 
      for word in "${distinct[@]}"; do
          echo "${freqs["$word"]}" "$word"
      done
  }
Used grep here because the original pipeline erroneously introduces an empty line when the file starts with a character that doesn't match '[:alpha:]'.