HOWTO remove empty lines with Regex

Discussion in 'Perl Misc' started by tor, Dec 10, 2003.

  1. tor

    tor Guest

    Hi
    I wan't to remove all empty lines in a file.
    How can I do that with a regex:
    s/<something>//;
    What is this <something>

    Torfinn
     
    tor, Dec 10, 2003
    #1
    1. Advertising

  2. tor

    Anno Siegel Guest

    tor <> wrote in comp.lang.perl.misc:
    > Hi
    > I wan't to remove all empty lines in a file.
    > How can I do that with a regex:
    > s/<something>//;
    > What is this <something>


    Apparently you want to remove empty lines not from a file, but a string.

    There may be such a <something>, but it's easier with a different regex.

    s/\n+/\n/g

    does it.

    tr/\n//s

    does it faster.

    Anno
     
    Anno Siegel, Dec 10, 2003
    #2
    1. Advertising

  3. tor <> wrote:

    > I wan't to remove all empty lines in a file.



    perl -n -i.old -e 'print unless /^\s+$/' file


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Dec 10, 2003
    #3
  4. Tad McClellan <> wrote:
    > tor <> wrote:
    > > I wan't to remove all empty lines in a file.

    >
    > perl -n -i.old -e 'print unless /^\s+$/' file


    Nitpick mode: remove empty lines too:
    perl -n -i.old -e 'print unless /^\s*$/' file
    ^
    ^
    --
    Glenn Jackman
    NCF Sysadmin
     
    Glenn Jackman, Dec 10, 2003
    #4
  5. tor

    tor Guest

    Clearification--> HOWTO remove empty lines with Regex

    This might clearify
    I got this file:
    -----------------
    Line 1<newlin>
    Line 2<newlin>
    <newlin>
    Line 4<newlin>
    ----------------
    I want this
    -----------------
    Line 1<newlin>
    Line 2<newlin>
    Line 4<newlin>
    ----------------

    I want to remove the line who only contains <newline>,
    so that the array @var only lines that contains data.

    while(<FILE>) {
    s/<something>/<nothing>/;
    #Where <something> the regular expression I'm missing
    push(@var,$_);
    }



    "tor" <> wrote in message
    news:3fd72270$0$9780$...
    > Hi
    > I wan't to remove all empty lines in a file.
    > How can I do that with a regex:
    > s/<something>//;
    > What is this <something>
    >
    > Torfinn
    >
    >
     
    tor, Dec 10, 2003
    #5
  6. Re: Clearification--> HOWTO remove empty lines with Regex

    tor wrote:
    > This might clearify
    > I got this file:
    > -----------------
    > Line 1<newlin>
    > Line 2<newlin>
    > <newlin>
    > Line 4<newlin>
    > ----------------
    > I want this
    > -----------------
    > Line 1<newlin>
    > Line 2<newlin>
    > Line 4<newlin>
    > ----------------
    >
    > I want to remove the line who only contains <newline>,
    > so that the array @var only lines that contains data.
    >
    > while(<FILE>) {
    > s/<something>/<nothing>/;
    > #Where <something> the regular expression I'm missing
    > push(@var,$_);
    > }


    In that case, you are on the wrong track with that s/// operator. You
    can just do:

    while (<FILE>) {
    push @var, $_ unless $_ eq "\n";
    }

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Dec 10, 2003
    #6
  7. On 10 Dec 2003, Glenn Jackman wrote:

    >Tad McClellan <> wrote:
    >> tor <> wrote:
    >> > I wan't to remove all empty lines in a file.

    >>
    >> perl -n -i.old -e 'print unless /^\s+$/' file

    >
    >Nitpick mode: remove empty lines too:
    > perl -n -i.old -e 'print unless /^\s*$/' file


    But \n is matched by \s, and the $ anchor doesn't HAVE to match a newline,
    so on a string like "\n", both /^\s+$/ and /^\s*$/ match.

    --
    Jeff Pinyan RPI Acacia Brother #734 2003 Rush Chairman
    "And I vos head of Gestapo for ten | Michael Palin (as Heinrich Bimmler)
    years. Ah! Five years! Nein! No! | in: The North Minehead Bye-Election
    Oh. Was NOT head of Gestapo AT ALL!" | (Monty Python's Flying Circus)
     
    Jeff 'japhy' Pinyan, Dec 10, 2003
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tim Haynes

    How to remove empty lines with re?

    Tim Haynes, Oct 10, 2003, in forum: Python
    Replies:
    9
    Views:
    410
    Klaus Alexander Seistrup
    Oct 12, 2003
  2. christek
    Replies:
    1
    Views:
    335
    Gordon Beaton
    Jan 31, 2007
  3. Replies:
    3
    Views:
    796
    Reedick, Andrew
    Jul 1, 2008
  4. Chris Withers
    Replies:
    2
    Views:
    493
    Chris Withers
    Aug 11, 2010
  5. Replies:
    7
    Views:
    194
    Ben Morrow
    Jun 7, 2006
Loading...

Share This Page