We help IT Professionals succeed at work.
Get Started

deleting last line in file if not blank

NorthReptile asked
Last Modified: 2012-05-09
I am logging to an XML file which continually appends and I want to remove the last root tag so that the XML validates when I open it in a web browser, for example.

I am trying to delete the last entry in a log file and found this code snippet which achieved that

    open (FH, "+< $logFile")               or die "can't update $file: $!";
    while ( <FH> ) {
        $addr = tell(FH) unless eof(FH);
    truncate(FH, $addr)                 or die "can't truncate $file: $!";

However, if there are blank lines it only removes the blank line. I want to remove any lines up and including the last </log> tag

I'd prefer a clean perl script but bash would be fine too
Watch Question
This problem has been solved!
Unlock 1 Answer and 4 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE