Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Deleting first and last lines of a text file

by DrHyde (Prior)
on May 16, 2014 at 10:48 UTC ( #1086285=note: print w/replies, xml ) Need Help??


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

HEAD=`head -1 myfile.txt` TAIL=`tail -1 myfile.txt` grep -v $HEAD myfile.txt|grep -v $TAIL>myfile.txt~ mv myfile.txt~ myfile.txt

Untested code. Needs some debugging.

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

    That'll not just delete the first and the last line, but also any line that happens to be the same. I.e., it'll turn this:

    the quick brown fox jumps over the lazy dog's back

    into this:

    quick brown fox jumps over lazy dog's

    rather than this:

    quick brown fox jumps over the lazy dog's
Re^2: Deleting first and last lines of a text file
by vsmeruga (Acolyte) on May 16, 2014 at 11:02 UTC

    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

      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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2019-04-21 14:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I am most likely to install a new module from CPAN if:
















    Results (111 votes). Check out past polls.

    Notices?