#!/bin/sh -e
#
# indextext - create index to text in which each word is followed by line numbers on which it occurs

if [ -z "$1" ]; then
  printf '%s\n' "$(basename "$0") [file]"
  exit 1
else
  cat -- "$1" \
    | tr -dc '[A-Za-z][:blank:]\012' \
    | gawk '
  {
    for (i = 1; i <= NF; i++) words[$i] = words[$i]  sprintf(", %d",NR)
  }
  END{ for (i in words) {
        lines = words[i];
        sub(/^,/,"",lines);
        printf("%s\t%s\n",i,lines);
  }
}' | sort -f -k 1 >"$1".index
  if test -e "$1".index; then
    bat "$1".index | align "\t"
  fi
fi
