Re: Rewriting into file
by davido (Cardinal) on Jun 20, 2011 at 06:24 UTC
|
You can do it using one of the easy ways, or one of the hard ways. The hard way is to open the file for read/write, read the target line, formulate a replacement line of exactly the same number of bytes, seek back to the start of the line, and write it back out. Be sure to obtain a file lock while you're there.
An easier way would be to open a temporary output file (see File::Temp), read the input file, simultaneously writing to your temp file. When you get to the line(s) you need to change, make your change and write them out to the output file too. Continue through to the end of the file. Close both files. Rename the output file over the input file. It would be wise to test your output thoroughly, at least while in development to make sure your plan worked before you go clobbering your input file. If there's any possibility your input file could get modified while you're working be sure to obtain a lock.
Another easy solution is to use Tie::File. Find the line(s) of interest, make your change, and make your changes. Tie::File handles some of the details without you dealing with them. But locking could still be important in a multi-user environment.
I would prefer the second solution if it were me. An alternate on that strategy would be to rename the input file to input.name.bak, iterate over it, writing to a new file with the original file's name. Now you've got a backup.
| [reply] [Watch: Dir/Any] |
Re: Rewriting into file
by dHarry (Abbot) on Jun 20, 2011 at 08:36 UTC
|
You can also use the -i command-line switch to modify a file in place. See perlrun for details.
(Well it's cheating a little bit, you would still use a temporary file but Perl takes care of the details.)
Cheers
dHarry
| [reply] [Watch: Dir/Any] |
Re: Rewriting into file
by wind (Priest) on Jun 20, 2011 at 06:03 UTC
|
| [reply] [Watch: Dir/Any] |
|
Yes,but how can i only replace the particular value.
eg
I have a file acb.txt and its contents are:
P|abcd
now i want acbd to be capitalized to ABCD and the then my file should look like
P|ABCD
| [reply] [Watch: Dir/Any] |
Re: Rewriting into file
by Anonymous Monk on Jun 20, 2011 at 06:40 UTC
|
Can anyone of you guide me?
What is it that you're really trying to accomplish?
If you're trying to get the job done, editing a file is fours steps
- read original-file
- modify data
- write new-file
- rename new-file to original-file
If on the other hand this is a homework assignment, read open, Tie::File, and most important, ask your teacher what you're supposed to learn from this exercise; you're cheating yourself by asking us. | [reply] [Watch: Dir/Any] |
|
Hello,
No this is not an homework assignment.I know that we can write to a new file,but i was just wondering if we can rewrite into the same file and just replace that particular value.
| [reply] [Watch: Dir/Any] |
|
| [reply] [Watch: Dir/Any] |
Re: Rewriting into file
by 7stud (Deacon) on Jun 20, 2011 at 09:02 UTC
|
You can in this case, but you should just forget about doing that. The general rule is: you cannot alter files. Learn the four step process posted above.
After you learn to manually do the 4-step process, then you can use a shortcut for that process that perl provides:
use strict;
use warnings;
use 5.010;
use English;
{
#Turn on inplace editing for <> operator, no backup file:
local $INPLACE_EDIT = "";
local @ARGV = "./test/data.txt";
#...or list file name on command line
while(my $line = <>) {
$line =~ s/hello/goodbye/; #alter the line
print $line; #...then print() it (*required*)
}
}
| [reply] [Watch: Dir/Any] [d/l] |
|
| [reply] [Watch: Dir/Any] |
|
Hi All,
I completed the above task.
Thanks All for the help.
| [reply] [Watch: Dir/Any] |