in reply to How to delete a particular line in a text file after some operation on that text file at the same time

Hello ppp,

Please see the entry “How do I change, delete, or insert a line in a file, or append to the beginning of a file?” in perlfaq5. Note the following:

The basic idea of inserting, changing, or deleting a line from a text file involves reading and printing the file to the point you want to make the change, making the change, then reading and printing the rest of the file. Perl doesn't provide random access to lines...

Once the new file has been written successfully, delete the original and rename the new file using either the builtin rename function or the move function from File::Copy.

Hope that helps,

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

  • Comment on Re: 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^2: 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 04:21 UTC
    Hii, Thanks for the answer. please see the EDIT part in my answer i tried it but it deltes all the text file.
      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,

        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: $!";
        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.