in reply to Re: Re: Deleting a line out of text file
in thread Deleting a line out of text file
You need to print out all the other lines also.
If you want to edit a line and leave it in the file then just get rid of the if.
... 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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re:^4 Deleting a line out of text file
by kesterkester (Hermit) on Aug 13, 2003 at 19:03 UTC | |
|
Re: Re: Re: Re: Deleting a line out of text file
by Anonymous Monk on Aug 13, 2003 at 17:53 UTC | |
by dga (Hermit) on Aug 13, 2003 at 21:24 UTC |