Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

removing blank lines from files

by Anonymous Monk
on Jun 20, 2002 at 12:36 UTC ( [id://175985]=perlquestion: print w/replies, xml ) Need Help??

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

I am reading in a file of data and sorting it into catergories...anyway, my problem is that i need to remove some whole lines from the file which contain nothing;
e.g ttttttttttttttttttttt ttttttttttttttttttttttt ttttttttttttttttttttt ttttttttttttttttttttt
can anyone explain how to get rid of these lines, i have tried chomp; but still theres a problem.

Replies are listed 'Best First'.
Re: removing blank lines from files
by zakb (Pilgrim) on Jun 20, 2002 at 12:44 UTC

    Perhaps those "blank" lines contain spaces? Also, using chomp simply removes line endings (e.g. carriage return), it won't skip to the next line. You haven't really shown us your code, but presumably you need something like:

    #!/usr/bin/perl -w use strict; while (<>) { # loop to next line if it contains whitespace only next if /^\s+$/; print; }

    Update: Tested with files with blank lines consisting of just \n and blank lines with spaces; works in both cases; \s+ matches end-of-line characters, see perlretut (query from mirod & busunsl).

      Change
      next if /^\s+$/;
      to
      next if /^\s*$/;
      and it will catch empty lines and lines containing only whitespace.

        Actually Perl DWIMs properly here, and /^\s+$/ catches empty lines. The $ is quite magic, if the \n at the end of the string/line is matched then $ matches the empty end-of-string, if the \n is not matched then $ matches it, plus the end-of-string.

        You can test it with this code:

        my $string="12\n"; print 'matches ^\d+\n$' . "\n" if( $string=~ /^\d+\n$/); print 'matches ^\d+$' . "\n" if( $string=~ /^\d+$/);
Re: removing blank lines from files
by marvell (Pilgrim) on Jun 20, 2002 at 12:49 UTC
    perl -i.bak -ne 'print unless /^\s*$/' file

    --
    Steve Marvell

      To shorten your logic a bit:
      perl -ni.bak -e 'print if /\S/' file1 file2 ...
      In other words, "print if there is non-whitespace" versus "print unless composed of only whitespace".

        Update:To the benefit of speed too:

        #!/usr/bin/perl -w use strict; use Benchmark qw(timethese); my @data = ("sponge\n","\n", "wibble\n","boing\n", "spam\n","\n"," spud\n"); my $nsre = qr/\S/; my $isre = qr/^\s+$/; timethese(1E6, { 'nospace' => sub { grep {/$nsre/} @data; }, 'isspace' => sub { grep {!/$isre/} @data; }})

        Resulting in:

        Benchmark: timing 1000000 iterations of isspace, nospace... isspace: 67 wallclock secs (56.34 usr + 0.15 sys = 56.49 CPU) @ 17 +702.25/s (n=1000000) nospace: 63 wallclock secs (49.19 usr + 0.26 sys = 49.45 CPU) @ 20 +222.45/s (n=1000000)

        Unless my dataset is unfair.

        --
        ¤ Steve Marvell

Re: removing blank lines from files
by jmcnamara (Monsignor) on Jun 20, 2002 at 13:11 UTC

    Here is one other (tongue in cheek) method:     perl -i.bak -pe 'goto LINE unless /\S/' file

    --
    John.

Re: removing blank lines from files
by Bilbo (Pilgrim) on Jun 20, 2002 at 12:46 UTC
    To get rid of lines containing nothing at all you could do something like this:
    #!/usr/bin/perl -w use strict; while (<>) { next if (/^\n/); print; }
    where the regex matches lines which start with a newline chartacter. If you also wanted to ignore lines which contained only spaces you could try replacing the regex line with
    next if (/^\s*$/);
Re: removing blank lines from files
by gumby (Scribe) on Jun 20, 2002 at 12:48 UTC
    chomp will remove any trailing string that corresponds to the value of $/ (see perlfunc). So you want something like s/^\s*$//g. Some code would be helpful here by the way.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://175985]
Approved by claree0
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (6)
As of 2024-03-28 16:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found