#!/bin/bash script=:1 for l in {a..z} do script+=";s/$l${l^}//g;s/${l^}$l//g" done shrunk=$(sed -e "$script;t1" input.txt) echo ${#shrunk}