use strict; my $file_name="c:\\temp.txt"; open (FH,"$file_name"); my $pattern="some thing"; my $c=0; my $space=' '; my $line; while() { $line=$_; #current line $c++; #counts the number of lines $space=' ' x $c .' ' x length($pattern); #pattern that has to be replaced $line=~s/$pattern/$space/i; }