in reply to Deleting first and last TWO(2) lines of a text file
Something like this should work:
#!/usr/bin/perl <>; while($line = <>) { print if defined; $_ = $line; }
Explanation: the angle bracket operator reads a line from a while, so the first line is read and discarded. After that, $_ (Perl's implicit topic variable) is used to store the previous line; it's initially undefined, so the print statement doesn't get executed on the first loop iteration, only the subsequent ones. Since only the previous line gets printed, not the current line, the last line of the file is skipped.
EDIT: here's a slightly more idiomatic solution:
#!/usr/bin/perl <>; while(<>) { last if eof; print; }
It's pretty much the same as before, but does away with remembering the previous line and instead just exits the loop before printing if the end of the file has been reached, thus neglecting to print the last line.
EDIT 2: just so there's no confusion in the future, the OP originally asked for the first and last line of a text file to be removed, not the first line and last two lines.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Deleting first and last lines of a text file
by vsmeruga (Acolyte) on May 16, 2014 at 09:45 UTC | |
by AppleFritter (Vicar) on May 16, 2014 at 10:52 UTC | |
|
Re^2: Deleting first and last lines of a text file
by vsmeruga (Acolyte) on May 16, 2014 at 13:06 UTC | |
by AppleFritter (Vicar) on May 16, 2014 at 19:00 UTC |