Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Rewriting into file

by pr09 (Novice)
on Jun 20, 2011 at 05:47 UTC ( [id://910498]=perlquestion: print w/replies, xml ) Need Help??

pr09 has asked for the wisdom of the Perl Monks concerning the following question:

Hi All, I have a situation where i have to read a file for a particular value,convert the value into capital letters and then again rewrite into file. Now,i first opened a file in read mode and split a line and then converted into upper case using uc.But i want to only replace that particular field and not the whole file.Also i don't want to create a new file too. Can anyone of you guide me?

Replies are listed 'Best First'.
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.


    Dave

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

Re: Rewriting into file
by wind (Priest) on Jun 20, 2011 at 06:03 UTC
      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
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.
      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.
        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.

        A take-home-test is an homework assignment.

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*) } }
        Hi All, I completed the above task. Thanks All for the help.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://910498]
Approved by lidden
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (2)
As of 2024-03-19 07:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found