-----------------8<---------8<------------------ use strict; use warnings; my $filename = 'tablet2.data1.txt'; open( IN, "<$filename" ) or die "Unable to open input file '$filename': $!"; local $/ = undef; my $buf = ; my $pattern = 'The text to be searched\s*in differnt lines inclunding\s*white lines and indentation\s*'; my $replace = <<'EOF'; It seems like magic but it's really just waving wands 'n stuff EOF $buf =~ s/$pattern/$replace/; print $buf; close(IN); -----------------8<---------8<------------------ use strict; use warnings; use Tie::File; # tie @array to filename using Tie::File tie my @array, 'Tie::File', 'result.txt' or die "Cannot open result.txt:$!"; #while( ) { for(@array) { my $pattern = "The text to be searched in differnt lines inclunding white lines and indentation"; my $replace = " The replacing text also in multiple lines including white spaces and white lines "; $pattern = '\s\s'; $replace = '||'; s/$pattern/$replace/g; } #} untie @array;