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

Hi I tried with below code calling scripts as ./scriptname srcfile.txt. But it deleted just the first line. Not last 2 lines/1 line </p?

#!/usr/bin/perl my $file='jjl.exec.txt'; open STDOUT, ">", $file or die "$0: open: $!"; open STDERR, ">&STDOUT" or die "$0: dup: $!"; <>; while($line = <>) { print if defined; $_ = $line; }

Replies are listed 'Best First'.
Re^3: Deleting first and last lines of a text file
by AppleFritter (Vicar) on May 16, 2014 at 19:00 UTC

    Hmm, that should... and is, in fact, working for me. Are you sure there's not simply an empty line at the end of your input file? Perhaps you could share some sample data.

    Also, why the open calls? If you want the output to go to a specific file, it'd be easier to simply redirect the script's output on the command line, like so:

    $ ./scriptname srcfile.txt >jjl.exec.txt

    Alternatively, I'd suggest at least using a different filehandle than STDOUT, though you'd of course have to adjust the print statement to print to that filehandle then. opening STDOUT like that apparently works, but it's giving me the heebies.