in reply to Re^2: 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

while (my $line = <$fhi>) { //Here i write the code to get the time from text file in ech li +ne and below i check in if condition if that exceed 34 hours then flu +sh that line if($Time >'34') { $fhi->autoflush; } print $fho; }

The autoflush function isn’t going to help you here. You need logic like this (untested):

while (my $line = <$fhi>) { # get $time print $fho $line unless $time > 34; }

That is: print to the output file only those lines which pass the test, and silently ignore those which don’t. And note that your statement: print $fho; needs to be changed to print $fho $line; in order to work correctly here.

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

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

Replies are listed 'Best First'.
Re^4: How to delete a particular line in a text file after some operation on that text file at the same time
by ppp (Acolyte) on Jan 18, 2015 at 05:08 UTC
    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.

      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".

      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
Re^4: How to delete a particular line in a text file after some operation on that text file at the same time
by ppp (Acolyte) on Jan 18, 2015 at 05:36 UTC
    I also tried this but out of luck :
    copy("C:\shekhar_Axestrack_Intern\WindowCreation\ListOfIpAdress2.txt", +"C:\shekhar_Axestrack_Intern\WindowCreation\ListOfIpAdress.txt") or d +ie "Copy failed: $!";