#!/usr/local/bin/perl -i while ($line = <>) { while ($line =~ s#\d+\s*[*+-/]\s*\d+(\s*[*+-/]\s*\d+)*#<x>#) { eval ("\$result = $&;"); $line =~ s/<x>/$result/; } print ($line); }