Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Deleting first and last lines of a text file

by morgon (Deacon)
on May 16, 2014 at 09:32 UTC ( #1086269=note: print w/ replies, xml ) Need Help??


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


Comment on Re: Deleting first and last lines of a text file
Download Code
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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1086269]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (13)
As of 2015-09-01 15:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred temperature scale is:










    Results (30 votes), past polls