perl -i.bak -nlwe '$h{$_}++; if (eof) {print for sort { length($a) <=> length($b) } keys %h}' junk.txt junk2.txt