?
Solved

delete last 2 lines

Posted on 1999-11-29
10
Medium Priority
?
294 Views
Last Modified: 2010-03-05
anyone know the perl to open a file
and delete the last two lines?
thanks
paul
0
Comment
Question by:paulwhelan
  • 6
  • 4
10 Comments
 
LVL 5

Expert Comment

by:prakashk021799
ID: 2240970
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
 

Author Comment

by:paulwhelan
ID: 2246536
that doesnt seem to delte a line for me
paul
0
 
LVL 5

Expert Comment

by:prakashk021799
ID: 2246846
What does it delete then? part of the line? more than a line? nothing at all?

More details please.
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 

Author Comment

by:paulwhelan
ID: 2246857
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
 
LVL 5

Expert Comment

by:prakashk021799
ID: 2246881
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
 

Author Comment

by:paulwhelan
ID: 2246896
the three lines i posted are all i had in my file
do i need more
ie
close (FH);
0
 
LVL 5

Accepted Solution

by:
prakashk021799 earned 200 total points
ID: 2246904
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
 
LVL 5

Expert Comment

by:prakashk021799
ID: 2246907
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
 
LVL 5

Expert Comment

by:prakashk021799
ID: 2247124
So, what was the problem in your code?
0
 

Author Comment

by:paulwhelan
ID: 2247144
i didnt have the line
$addr = tell(FH);
in the loop
thanks!
paul
0

Featured Post

Get your problem seen by more experts

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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

601 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question