$/="["; while(<>) { ($newprefix) = m{uri.*([^/]*)$}m; if ($newprefix) { # we won't match on the first line so it will print just "[" print $newprefix," "; } print; }