http://www.perlmonks.org?node_id=1086269


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

perl -i.old -ne 'print unless $.==1 or eof' <input-file>
This will also keep the old version around as <input.file>.old

Replies are listed 'Best First'.
Re^2: Deleting first and last lines of a text file
by vsmeruga (Acolyte) on May 16, 2014 at 09:47 UTC

    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

      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
        But where are we specifying the filename here ?. I mean from which file first and last lines has to be deleted? Thanks VJ
Re^2: Deleting first and last lines of a text file
by vsmeruga (Acolyte) on May 16, 2014 at 09:36 UTC
    Thanks for the reply. I will try and let you know