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

This node falls below the community's minimum standard of quality and will not be displayed.
  • Comment on how to update a field of a record in a large text file

Replies are listed 'Best First'.
Re: how to update a field of a record in a large text file
by davorg (Chancellor) on Mar 07, 2006 at 11:24 UTC
Re: how to update a field of a record in a large text file
by CountZero (Bishop) on Mar 07, 2006 at 10:37 UTC
    It all will depend on how your text file is organised. If it is like a CSV-type of file, you could have a look at DBI and DBD::CSV.

    Otherwise, you would have to read in the file on a record-by-record basis (saving them immediately in a temporary file), until you reach the record you need, parse out the fields of that record, change the field, save this record and then reading and saving all remaining records. Delete the original file and rename the temporary file to the original name.

    Attention: only one process/user can access the file at a time, so you will have to take care of locking the file while you are using it or otherwise "Bad Thing Will Happen"™. It is one of the benefits of using a database that you don't have to take care of that issue.

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Re: how to update a field of a record in a large text file
by mickeyn (Priest) on Mar 07, 2006 at 10:36 UTC
    assuming you are familiar with the use of regular expressions (see 'perldoc perlre' if not), what you want is to perform a substitution using regular expression on this record.

    to do it on a text file - I recommend tie'ing the file to an array and perform the substitution on the array. take a look at Tie::File.

    example:

    use Tie::File; tie @array, 'Tie::File', 'test'; foreach (@array){ s/RECORD/NEW RECORD/g; } untie @array;

    Enjoy,
    Mickey

      That supposes of course that you know beforehand the content of the record to be searched (perhaps you only know its ID) and enough info on its fields to construct the regex to immediately replace the data of the field to be changed.

      CountZero

      "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Re: how to update a field of a record in a large text file
by prasadbabu (Prior) on Mar 07, 2006 at 10:30 UTC

    Then how to do it ???

    Like this???

    Without showing any sample data or coding, how can you expect a correct solution?

    Prasad

      all i need is which commands or syntax will help me out in this . I have about 500000 records of record length 600 each . suppose on 25000th record i find that i have to change the 256th position. The value previously held is "N".now i want to move "Y" to it . Please help.
        Do you have an index on this file? In other words, do you know which record you need? If that is the case, you can use seek to go straight to the position you need in the file.

        CountZero

        "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law