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

Hi I tried the code like this and got error as

syntax error at ./perl_deletelines.pl line 5, near "-ne" Execution of ./perl_deletelines.pl aborted due to compilation errors. </P

#!/usr/bin/perl use strict; use warnings; perl -i.old -ne 'print unless $.==1 or eof' /home/file_20140407.txt

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

    The script given to you by morgon is a oneliner, which you should "run" from your command line interface, not from your a script.
    If you want a full script of the oneliner this would do:

    BEGIN { $^I = ".old"; } LINE: while ( defined( $_ = <ARGV> ) ) { print $_ unless $. == 1 or eof; }
    putting the above in a script should work.

    Of course, morgon code works fine from the CLI.

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

        .. 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
      But where are we specifying the filename here ?. I mean from which file first and last lines has to be deleted? Thanks VJ

        But where are we specifying the filename here ?...
        "We" are using ARGV, the special filehandle that iterates over command-line filenames in @ARGV, from the CLI.

        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