delete last 2 lines

anyone know the perl to open a file
and delete the last two lines?
thanks
paul
paulwhelanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

prakashk021799Commented:
One way is to open the file, read everything but the last two lines, write them in a temp file and copy the temp file over to the original.

Another way is suggested by
perldoc -q 'delete a line'

.....

In the unique case of deleting lines at the end of a file, you
can use tell() and truncate().  The following code snippet deletes the last line of a file without making a copy or reading the whole file into memory:

        open (FH, "+< $file");
        while ( <FH> ) { $addr = tell(FH) unless eof(FH) }
        truncate(FH, $addr);

Error checking is left as an exercise for the reader.




The above is an example of deleting the last line in a file. You will need to change this code to delete last two lines.
0
paulwhelanAuthor Commented:
that doesnt seem to delte a line for me
paul
0
prakashk021799Commented:
What does it delete then? part of the line? more than a line? nothing at all?

More details please.
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

paulwhelanAuthor Commented:
nothing at all...
i changed it to

open (FH, "+< ../htdocs/deletefile.html");
while ( <FH> ) { $addr = tell(FH) unless eof(FH) }
                             truncate(FH, $addr);


would that be the problem?
paul
0
prakashk021799Commented:
I tested the code with a test file containing 10 lines. After I ran the code, I saw only 9 lines in the file. Works for me.

Did you check the return value of open?
Did you remember to close the file, just in case, after truncate?
0
paulwhelanAuthor Commented:
the three lines i posted are all i had in my file
do i need more
ie
close (FH);
0
prakashk021799Commented:
I tried changing the code so that it deletes the last two lines and it too worked for me. Here's the code:

$file = "../htdocs/deletefile.html";
open (FH, "+< $file") or die "can't open $file: $!\n";
$addr = tell(FH);
while ( <FH> ) {
    unless (eof(FH)) {
        $prev_addr = $addr;
        $addr = tell(FH);
    }
}
truncate(FH, $prev_addr);
close(FH);

This code deletes the last two lines.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
prakashk021799Commented:
close is not really mandatory, althought it is preferred to have it there for clean closing.

What about checking the return value of open?
0
prakashk021799Commented:
So, what was the problem in your code?
0
paulwhelanAuthor Commented:
i didnt have the line
$addr = tell(FH);
in the loop
thanks!
paul
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.