c:\@Work\Perl\monks>perl -wMstrict -le "use 5.010; ;; my $t = qq{ .....P1p1/6P1/7P/5PK1/8 w - - 4 34]\n} . qq{ {Weis am Zug} * .....\n} . qq{1n5P/1P4PK/1q6 b - - 2 42]\n} . qq{ {Weis am Zug} * .....\n} . qq{.......5K2/3R4 w - - 1 33]\n} . qq{ {Weis am Zug} *\n} ; print qq{[[$t]]}; ;; my $pre = qr{ \S+ \s }xms; my $post = qr{ \s [^\n]+ \n [^\{]+ \{ }xms; ;; $t =~ s{ ^ $pre b $post \K Weis \b} {Schwarz}xmsg; print qq{[[$t]]}; " [[ .....P1p1/6P1/7P/5PK1/8 w - - 4 34] {Weis am Zug} * ..... 1n5P/1P4PK/1q6 b - - 2 42] {Weis am Zug} * ..... .......5K2/3R4 w - - 1 33] {Weis am Zug} * ]] [[ .....P1p1/6P1/7P/5PK1/8 w - - 4 34] {Weis am Zug} * ..... 1n5P/1P4PK/1q6 b - - 2 42] {Schwarz am Zug} * ..... .......5K2/3R4 w - - 1 33] {Weis am Zug} * ]] #### use 5.010; ... my $pre_b = qr{ ... }xms; my $pre_weis = qr{ ... }xms; my $b_was_seen; while (my $line = <$input_filehandle>) { # check for trigger condition/substitution pattern sequence. # ASSUME: 'b' and 'Weis' cannot both occur in same line. if ($line =~ m{ \A $pre_b b }xms) { # flag 'b' was seen in this line. $b_was_seen = 1; } elsif ($b_was_seen) { # attempt substitution if 'b' seen in previous line. $line =~ s{ \A $pre_weis \K Weis }{Schwarz}xms; $b_was_seen = 0; } # write each (possibly altered) line to output file. print $output_filehandle $line; }