2 Replies - 5362 Views - Last Post: 14 September 2012 - 07:51 AM

#1 blindTiger   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 12-April 12

Write to same file

Posted 13 September 2012 - 12:38 PM

Hello,

I am reading a file and replacing part of each line with something else and when I execute my code I pipe the output to a new file to create a new file with the updates. is there a way to do it so that we can update the same file, to not create a new file?

my ($sakRecip, $replString, $replDOB, $replSSN, $idMed, $query);
$sakRecip = 0;
$replString = ' ';
$replDOB = '        ';
$replSSN = '         ';

while(<MYINPUTFILE>) {
    
    my($line) = $_;
    
    chomp($line);
    
    if(length($line) < 36)
    {
       print "$line\n";
    }
    else
    {
        $idMed = substr($line, 671, 12);
        $sakRecip = findSakRecip();
        
        $replString = sprintf( '%11s', $sakRecip);
        substr($line, 671, 11) = $replString;
        substr($line, 881, 8) = $replDOB;
        substr($line, 890, 9) = $replSSN;
        print "$line\n";
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Write to same file

#2 dsherohman   User is offline

  • Perl Parson
  • member icon

Reputation: 227
  • View blog
  • Posts: 654
  • Joined: 29-March 09

Re: Write to same file

Posted 14 September 2012 - 01:06 AM

In the general case, it's not quite that simple. Given the way that files are stored on disk, you have to at least rewrite from the beginning of the change to the end of the file any time you replace a chunk of data with another chunk that's not exactly the same size. Even if they are the same size, you still have to rewrite the entire disk sector where the replaced data was stored.

So, while it can be done (start by looking up the documentation on the sysread, syswrite, and sysseek commands if you want to dig into it), it's generally not worth the trouble. Much easier to write a completely new file, then rename it to replace the original.
Was This Post Helpful? 0
  • +
  • -

#3 blindTiger   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 12-April 12

Re: Write to same file

Posted 14 September 2012 - 07:51 AM

View Postdsherohman, on 14 September 2012 - 01:06 AM, said:

In the general case, it's not quite that simple. Given the way that files are stored on disk, you have to at least rewrite from the beginning of the change to the end of the file any time you replace a chunk of data with another chunk that's not exactly the same size. Even if they are the same size, you still have to rewrite the entire disk sector where the replaced data was stored.

So, while it can be done (start by looking up the documentation on the sysread, syswrite, and sysseek commands if you want to dig into it), it's generally not worth the trouble. Much easier to write a completely new file, then rename it to replace the original.



Thanks for taking the time to look into. I'll research those functions
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1