use strict; use warnings; my $cell_name = 'xyzzy'; my $match = 'STARTING_PATTERN'; @ARGV = 'delme.txt'; $^I = '.bak'; while (<>) { s/$1/$cell_name/ if /$match\s+(.*)/; print; }