use strict; open INFILE,"infile.txt"; open OFILE,">outfile.txt"; my $total_os=0; while(){ my $tmp=$_; $str=~s/(\s+)/osmarker(pos($str),$1)/xeg; # a bunch of regular expressions $total_os+=length($_); print OFILE $str; } sub osmarker{ my $os=shift; my $spaces=shift; $os+=length($spaces)+total_os; return $spaces.""; } #### my $space=qr/(?:|\s)/;