Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Re: Deleting first and last lines of a text file

by morgon (Curate)
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

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 ./ line 5, near "-ne" Execution of ./ 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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1086269]
Corion idly wonders if there is a way to produce ordered .yml files. I want to accept YAML for a configuration file format, but I also want to generate (for debugging/start) a sample configuration file from the current configuration.
[Corion]: Ideally, that configuration file would have some order of the keys, but I'm not sure whether/how YAML supports ordered output.

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2017-01-16 11:56 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (149 votes). Check out past polls.