in reply to Re^3: How to delete a particular line in a text file after some operation on that text file at the same time
in thread How to delete a particular line in a text file after some operation on that text file at the same time

Hii thanks again for answer. But still u are not able to understand my problem. The output of your code will be done in another file. But i want to do it in same. I tried something like this :
open my $fhi, '<', 'C:\shekhar_Axestrack_Intern\WindowCreation\List +OfIpAdress.txt', or die "Could not open file $!"; open my $fho, '>', 'C:\shekhar_Axestrack_Intern\WindowCreation\Lis +tOfIpAdress2.txt', or die "Could not open file $!"; while (my $line = <$fhi>) { print "Total Difference in Hour is : $div \n\n"; print $fho $line unless $div > 36; } close $fhi; close $fho; rename 'C:\shekhar_Axestrack_Intern\WindowCreation\ListOfIpAdress2 +.txt', 'C:\shekhar_Axestrack_Intern\WindowCreation\ListOfIpAdress.txt +';

But as a result file ListOfIpAdress.txt is blank which was supposed to be replaced by ListOfIpAdress2.txt which we obtained from print $fho $line unless $time > 36; The problem is i want to have the output in the same file not in 2 different files.

  • Comment on Re^4: How to delete a particular line in a text file after some operation on that text file at the same time
  • Download Code

Replies are listed 'Best First'.
Re^5: How to delete a particular line in a text file after some operation on that text file at the same time
by soonix (Chancellor) on Jan 18, 2015 at 06:48 UTC
    The output of your code will be done in another file. But i want to do it in same.

    It's the same as in any other programming language:

    On a sheet of paper, you can use whiteout, but then there will remain a visible gap. If you don't want that, you have to rewrite (i.e. copy) the complete sheet.

    In Perl (or C, or whatever) , you could overwrite with the same number of bytes, e.g. spaces, but if you want to avoid the gap, you have to copy the whole thing into a new file and then do the "delete and rename" dance. That's what most editors and word processors do.

    Tie::File (see GotToBTru's reply) internally does, so to say, use the whiteout/overwrite approach and rewrites all subsequent lines "in place".

Re^5: How to delete a particular line in a text file after some operation on that text file at the same time
by james28909 (Deacon) on Jan 18, 2015 at 06:25 UTC
    You have toi return the position you are at in the file, then read up to that position into one variable. then skip to next line after match then read until the EOF into a different variable. then print those two variables to a different file, then delete old file, then rename new file. i do it with param.sfo's that are only a few kilobytes long, works great