in reply to Re^3: Deleting first and last lines of a text file
in thread Deleting first and last TWO(2) lines of a text file

Hi I should automate the task of deleting the lines in the file so that it runs every day and deletes particular file Thanks VJ
  • Comment on Re^4: Deleting first and last lines of a text file

Replies are listed 'Best First'.
Re^5: Deleting first and last lines of a text file
by 2teez (Vicar) on May 16, 2014 at 11:04 UTC

    .. should automate the task of deleting the lines in the file ..
    Then, in that case, you can specify the filename in your script, then open a filehandle to read from,doing all you wanted done. Like so:

    use warnings; use strict; my $filename = '...'; # specify the file name here open my $fh, '<', $filename or die "can't open $filename: $! "; while ( defined( $_ = <$fh> ) ) { print $_ unless $. == 1 or eof; } close $fh or die "can't close $filename: $!";

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me

      Thanks for your time. I tried with below code. but it deleted just first line. Can you please help me to delete last 2 lines as well

      #!/usr/bin/perl use strict; use warnings; my $file='tgtfile.txt'; open STDOUT, ">", $file or die "$0: open: $!"; open STDERR, ">&STDOUT" or die "$0: dup: $!"; my $filename = 'srcfile_20140319.txt'; # specify the file name here open my $fh, '<', $filename or die "can't open $filename: $! "; while ( defined( $_ = <$fh> ) ) { print $_ unless $. == 1 or eof; } close $fh or die "can't close $filename: $!";