You may not think you have the budget to do it right, but do you have the budget to do it wrong? I honestly don't think this is going to be easier to do wrong!

But back to the question. To "edit" a file like this you basically have to load the entire thing into memory, make your changes, and then write the whole thing back out. And you'll have to lock the file while you're doing it to make sure you don't get two editors working at the same time. The reason it's not possible to edit the file in-place is because the lines are of variable length, so editing means changing the length of the file and the position of lines lower down.

So, you'll start by loading all the data into a structure in memory. Something like (untested):

open (DB,"ext.log") or die("There was a problem opening the file."); my @data; while (my $entry=<DB>) { my @fields = split(/ /,$entry); push @data, \@fields; } close DB;

Now you'll make a change:

  $data[5][1] = "New value!";

Then write it all back out again:

open(DB, ">", "ext.log") or die $!; foreach my $row (@data) { my $line = join(' ', @$row); print DB $line, "\n"; } close DB;

If your file is it at all big this is going to totally suck. In that case you might look at using something like Tie::File. That will help with memory usage, but there's no helping the fact that making random edits to a sequential file will be slow.

Now doesn't this make you want to do it right? Good! You should be using a database of some kind to hold the data. If you have a real DB like MySQL or Postgres already available then use that. If not you might look at something like DBD::SQLite to save you the trouble of getting a real DB installed. Then create a table, load your log data into it, and suddenly editing lines will be cake! And who doesn't like cake?

-sam


In reply to Re: delete/over-write record in FlatFile by samtregar
in thread delete/over-write record in FlatFile by lakeTrout

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.