> perl -ln0777we '$c=0;s/\s+/ /sg;print" "x(40-($c=length$1)/2),$1 while s/(.{$c,}?. |.+$)//' a.txt w.txt a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a The same principle applies to any text file, basically splitting words only on spaces or new lines