Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^3: Deleting first and last lines of a text file

by 2teez (Vicar)
on May 16, 2014 at 10:06 UTC ( [id://1086278]=note: print w/replies, xml ) Need Help??


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

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

Replies are listed 'Best First'.
Re^4: Deleting first and last lines of a text file
by vsmeruga (Acolyte) on May 16, 2014 at 10:21 UTC
    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

        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: $!";
Re^4: Deleting first and last lines of a text file
by vsmeruga (Acolyte) on May 16, 2014 at 10:13 UTC
    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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2024-04-24 03:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found