#!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11129359 use warnings; use Path::Tiny; my $path = path( 'test3.txt' ); $path->spew(<edit_lines( sub { s/^(?=(foo_))/join '', map "$1$_\n", 1..3/e }); print $path->slurp; #### hello world foo_1 foo_2 foo_3 foo_ foo abc def