#!/usr/bin/perl while(<>) { chomp; /^\D*(\d)(?:\D*(\d))?\D*$/ or die "no head"; $sum += "$1" . ($2 || $1); } print "$sum\n";