perl -ne '/$regex/ and $SUM += $1; END { print $SUM }' file.txt