in reply to Find and replace based on unique identifier
#!/usr/bin/perl # https://perlmonks.org/?node_id=1220734 use strict; use warnings; my $new = <<END; Connection Culv=This is Line3 - New text here 333 333 This should be new too Conn Culvert Barrel=Culvert3 * Connection Culv=This is Line1 - New text here 111 111 This should be new too Conn Culvert Barrel=Culvert1 * Connection Culv=This is Line2 - New text here 222 222 This should be new too Conn Culvert Barrel=Culvert2 * END my $model = <<END; Connection Culv=This is Line1 111 111 Conn Culvert Barrel=Culvert1 * Connection Culv=This is Line2 222 222 Conn Culvert Barrel=Culvert2 * Connection Culv=This is Line3 333 333 Conn Culvert Barrel=Culvert3 * END my %replace; $replace{$2} = $1 while $new =~ /^Connection Culv=(.*\n.*\n)(Conn Culvert Barrel=.*\S)/gm; $model =~ s/^Connection Culv=\K.*\n.*\n(?=(Conn Culvert Barrel=.*\S))/ +$replace{$1}/gm; print $model;
Outputs:
Connection Culv=This is Line1 - New text here 111 111 This should be new too Conn Culvert Barrel=Culvert1 * Connection Culv=This is Line2 - New text here 222 222 This should be new too Conn Culvert Barrel=Culvert2 * Connection Culv=This is Line3 - New text here 333 333 This should be new too Conn Culvert Barrel=Culvert3
|
|---|