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 Gurus I just checked the file is having another extra line in the last "^Z" character. so I should remove last 2 lines and first line sorry for the correction. please help! Thanks VJ

  • Comment on Re^2: Deleting first and last lines of a text file

Replies are listed 'Best First'.
Re^3: Deleting first and last lines of a text file
by morgon (Priest) on May 16, 2014 at 12:00 UTC
    This deletes the fist line and the last 2 lines of input.txt and keeps a copy of the old version in input.txt.old.
    perl -i.old -ne 'my $l = <>; print($l), next if $.==2; print "$_$l" un +less eof' input.txt
    Note that this is a one-liner to be run as is from the command-line (adjust your input file-name).
      It is taking so much time and not exiting when I run it as a one liner.
      perl -i.old -ne 'my $l = <>; print($l), next if $.==2; print "$_$l" un +less eof' /home/file_20140319.txt