nanouk has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I'm starting to pull my hair out over this apparently simple problem: how to get rid of the trailing empty lines in a text file, no matter how many there are, while keeping a final carriage return? I can achieve what I want with sed but would like to do it in perl. Here's the sed one-liner:

sed -e :a -e '/^\n*$/{$d;N;ba' -e '}' /tmp/file.txt -i

Thanks in advance. Pascal

Replies are listed 'Best First'.
Re: delete empty lines from text file
by ikegami (Patriarch) on Nov 10, 2009 at 18:34 UTC
    perl -i -0777pe's/\n+\z/\n/' /tmp/file.txt
      Wow, great job! Thanks a million! Cheers, Pascal
Re: delete empty lines from text file
by BioLion (Curate) on Nov 10, 2009 at 18:26 UTC

    Perlhowto gives you a few options (one liners) : link and our own hallowed halls have a few ideas too : search...

    Just a something something...
Re: delete empty lines from text file
by Anonymous Monk on Nov 10, 2009 at 19:10 UTC
    check out s2p...