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

I searched the whole Perl Cookbook and found this as my possible solution. However, it does not want to remove the empty lines from the file read in. I must be missing something extremely simple here.
#!/usr/bin/perl open(LOG,"+</etc/spamdomains.txt") || die "Can't open spam file\n"; while ($nclean = <LOG>){ $nclean =~ s/^\s*(.?)\s*$/$1/; chomp $nclean; print "$nclean\n"; } close(LOG);

Replies are listed 'Best First'.
Re: Cannot seem to remove empty/blank lines
by zodiac (Beadle) on Jun 02, 2000 at 13:21 UTC
    It is as easy (and short) as this:
    perl -n -e "next unless /[^\s]/; print" /etc/spamdomains.txt
      Short you say? perl -pe 's/^\s*$//' /etc/spamdomains.txt
RE: Cannot seem to remove empty/blank lines
by t0mas (Priest) on Jun 02, 2000 at 12:15 UTC
    Try:
    open(LOG,"+</etc/spamdomains.txt") || die "Can't open spam file\n"; while (<LOG>) { /[^\s]+/ && print; } close(LOG);


    /brother t0mas
Re: Cannot seem to remove empty/blank lines
by Aighearach (Initiate) on Jun 02, 2000 at 11:20 UTC
    The problem is, you chomp the newline off, then print the cleaned string with a newline at the end. it works if you change

    print "$nclean\n";

    to

    if ( $nclean ) { print "$nclean\n"; }
    Paris Sinclair    |    4a75737420416e6f74686572
    pariss@efn.org    |    205065726c204861636b6572
    I wear my Geek Code on my finger.
    
      Thank you all, I understand what I was doing wrong now. It makes sense after you know how it works.
Re: Cannot seem to remove empty/blank lines
by mikkoh (Beadle) on Jun 02, 2000 at 11:21 UTC
    This one works, but I'm sure there's a more subtle approach..
    #!/usr/bin/perl open(LOG,"+</etc/spamdomains.txt") || die "Can't open spam file\n"; while (<LOG>){ unless(/^\s$){ print; } } close(LOG);
RE: Cannot seem to remove empty/blank lines
by marcos (Scribe) on Jun 02, 2000 at 13:39 UTC
    Just another solution:
    use strict; open(LOG,"+</etc/spamdomains.txt") || die "Can't open spam file\n"; my @lines = grep {!/^$/} <FILE>; close(LOG); print @lines;
    marcos
      PLEASE HELP!!!!!! I need to remove blank lines from the eml files I am creating using my script with net::pop3. Speciffically I would like to remove the blank line after the string "Content-Transfer-Encoding: 7bit" in the eml files. I will attach my script below. What it does is download mail, split it into .eml files with a random incremented number as the file name. However the format is messed up because of that space after that line. Thanks ##############################################
      use Net::POP3;
      # Constructors
      $pop = Net::POP3->new('', Timeout => 60);
      $i = int rand(10000000000000);
      if ($pop->login("", "") > 0) {
      my $msgnums = $pop->list; # hashref of msgnum => size
      foreach my $msgnum (keys %$msgnums) {
      open(MAILBOX, ">$i.eml")
      or die "Cannot open Mailbox file $timestamp$i.eml";
      print "Writing to $i.eml\n";
      $i++;
      my $msg = $pop->get($msgnum);
      print MAILBOX @$msg;
      # $pop->delete($msgnum);
      close(MAILBOX)
      or die "Cannot close mailbox";
      }
      }
      $pop->quit;<\code>