FAQ
I have some text files from which I would like to remove the first
line, but only if it's blank. Any hints? I tried a few things, but
the results haven't quite been satisfactory.

Thanks!

Search Discussions

  • Jim Gibson at Mar 7, 2014 at 10:10 pm

    On Mar 7, 2014, at 1:58 PM, sb@missionstclare.com wrote:

    I have some text files from which I would like to remove the first line, but only if it's blank. Any hints? I tried a few things, but the results haven't quite been satisfactory.
    You should be able to do that in just a few lines of Perl:

    1. Open the existing file for reading.
    2. Open a new file for writing.
    3. Read the first line in the input file.
    4. If the first line is not blank, write it to the output file.
    5. Read the remaining lines from the input file and write to the output file.
    6. Close both files
    7. Optionally: rename both files to give the new file the same name as the old file.

    If you don't understand any of the steps, just ask. If you have trouble, post your program here, and someone will be able to help you.

    Good luck.
  • Janek Schleicher at Mar 8, 2014 at 7:37 am

    Am 07.03.2014 22:58, schrieb sb@missionstclare.com:
    I have some text files from which I would like to remove the first line,
    but only if it's blank. Any hints? I tried a few things, but the
    results haven't quite been satisfactory.

    perl -pi -e '$_ = "" if ( $. == 1 && /^\n/);' filename

    on command line would be my first shot.


    Greetings,
    Janek

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedMar 7, '14 at 9:58p
activeMar 8, '14 at 7:37a
posts3
users3
websiteperl.org

People

Translate

site design / logo © 2021 Grokbase