perl -ne "BEGIN{print scalar <> for 1..14} END{print +(split /^/, $x)[-3..-1]} $x = $x x !/\QLX*/ . $_; /\QCAS*\E/ && print $x x $x =~ /00003/" "filename"