Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Script to update a value in text file

by amvarma (Initiate)
on Jun 15, 2010 at 05:39 UTC ( [id://844786]=perlquestion: print w/replies, xml ) Need Help??

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

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: Script to update a value in text file
by CountZero (Bishop) on Jun 15, 2010 at 05:53 UTC
    What have you tried yourself?

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: Script to update a value in text file
by Your Mother (Archbishop) on Jun 15, 2010 at 06:03 UTC
    perl -pe 's/\A([a-z]+)/$i=$1;++$i/e' [filename]

    And sno & cno will be incremented. Um… CountZero has a more useful, if less literal, reply, really.

Re: Script to update a value in text file
by biohisham (Priest) on Jun 15, 2010 at 06:03 UTC
    Welcome to the Monastery, Advising you to try to make your own script that does this job and then once you've fallen short we can pick it up from there as this site is not a code-writing service provider.

    Show us what you have tried so far and we'd pick it up from there, but for starters, think about a hash, it is not clear if you want an incrementing that is uniform or random since you've not mentioned that.

    Think about reading sno:10 and cno:10 into a hash and increment the hash values...

    Also relevant, read How do I post a question effectively?, Markup in the Monastery and Writeup Formatting Tips


    Excellence is an Endeavor of Persistence. A Year-Old Monk :D .
Re: Script to update a value in text file
by kejohm (Hermit) on Jun 15, 2010 at 06:33 UTC

    Try Tie::File for editing a file in place via a Perl array.

    Update: Link fixed.

Re: Script to update a value in text file
by Boldra (Deacon) on Jun 15, 2010 at 12:03 UTC
    in-place-edit is appropriate (YourMother's reply), but just because I'm still amazed at how cool IO::All is:
    perl -MIO::All -E 's/(\d+)/$1+1/e for @{io("/tmp/amvarma.txt")}'


    - Boldra
Re: Script to update a value in text file
by Marshall (Canon) on Jun 15, 2010 at 06:17 UTC
    The file on disk is a sequence of bytes. You can't simply insert or delete bytes because all the bytes after that have to be moved.

    For a small file, read the file into memory, make modifications as you will and then re-write the file. You may want to make some sort of backup scheme so that if your program dies while re-writing the file, the orginal data can still be recovered (i.e. don't delete, file and re-write with the same name because if that write fails, all data is lost).

Re: Script to update a value in text file
by JavaFan (Canon) on Jun 15, 2010 at 10:23 UTC
    Can I get some help, how to go about in implementing this?
    First, I'd learn a language of my choice. Then, in said language, I would:
    1. Get coffee (I recommend Java).
    2. Open the original file for reading.
    3. Open a new file for writing.
    4. Read in the original file, in chunks.
    5. For each chunk:
      1. Locate data I want to modify.
      2. Change the chunk so the value is incremented.
      3. Write the modified chunk to the new file.
    6. Close both open files.
    7. Move the new file over the original one.
    8. Finish coffee.
    9. Profit!
      6a. Rename original file (so it's still around for post mortems)
Re: Script to update a value in text file
by Khen1950fx (Canon) on Jun 16, 2010 at 01:09 UTC
    This increments sno and cno, but I did this just for fun.
    #!/usr/bin/perl use strict; use warnings; use Tie::CountLoop; tie my $counter, 'Tie::CountLoop', 'sno:10'; tie my $counter1, 'Tie::CountLoop', 'cno:20'; my $t = tied $counter; my $t1 = tied $counter1; $t->auto(1); $t1->auto(1); print " <<$counter>>:10 \n", " <<$counter1>>:20 \n";

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2024-04-23 22:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found