#! /bin/bash -eu
# Last edited on 2026-06-28 06:57:16 by stolfi

# Computes the counts of final letters of words that precede 
# words that start with a specified letter {letB}.
# Assumes that the input is all lowercase with no punctuation
# and words separated by blanks.

export LC_ALL=en_US.utf8
export LANG=en_US.utf8
export LC_CTYPE=en_US.utf8

letB="$1"; shift

echo "# -*- coding: utf-8 -*-"
echo "# -*- coding: utf-8 -*-" > .foo

cat \
  | sed \
      -e '/^[ ]*[#]/d' \
      -e '/^[ ]*$/d' \
      -e 's:<.*>::g' \
      -e 's:^[ ]*: :g' \
      -e 's:[ ]*$: :g' \
      -e 's:[ ][ ]*:  :g' \
      -e 's:[ ]\([^ ]\)[ ]: \1\1 :g' \
      -e 's:\([^ ]\)[ ][ ]*'["${letB}"']:@\1.'"${letB}"'@:g' \
  | gawk '//{ gsub(/[@]/, "\012", $0); print}' \
  | egrep -e '[.]' \
  | sort | uniq -c \
  | sort -b -k1,1nr 
