Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

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

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


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

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

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

    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: $!";

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2024-04-23 12:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found