...
foreach (@dat)
{
s/number/NEWWORD/;
print DATA $_;
}
close(DATA);
This will change the lines that need changing and print everything into the file.
There is another risk with this method that if the program dies before getting the file printed back out your original file which was erased on the second open will not have the complete data. This is what the backup options cover you against.
This will do the translation and leaves a backup file in this case named file.txt.bak.
perl -pi.bak -e 's/number/NEWWORD/;' file.txt
The -p says to print every line, the -i.bak says make a backup file by adding .bak to the input file name, -e says to run the next argument as a perl script. The substitute only happens if the pattern matched so there is no need for an if unless you want to locate one pattern and then replace another pattern with yet a different replacement.
|