If your numbers are in a file, a single line can be nice:
perl -lnwe 'my $sum; s/(\d)/$sum += $1/eg; print $sum' numbers.txt
Since adding uses only numbers, it is safe to ignore all other characters. So just extract them one at a time with the regular expression and sum them up.
TIMTOWTDI:
perl -MList::Util=sum -lnwe 'print sum(/\d/g);' numbers.txt perl -lnwe 'my $a; $a+=$_ for /\d/g; print $a' numbers.txt
Parameters:
-l enter auto-chomp and add a new line to print-n implicit while(<>) loop around the program - open the file name specified as an argument and read each line in $_ .
source share