Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

replacing blank lines

by indapa (Monk)
on Mar 06, 2001 at 01:19 UTC ( #62343=perlquestion: print w/replies, xml ) Need Help??

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

Hi, I am reading a .txt file with the following format:
1 1 Foo Bar Baz <blank line> 1 1 Foo Bar Baz
I would like to remove the blank lines btwn each record. I am trying the following code to do that:
while (<FILE>) { if (/(^$)/) { s/$1//; } }
I run the script with no errors, but when I look and see if the blank lines have been removed from the .txt file, there is no change. I have been trying debugging statements to see if the regex does indeed match, and it does. But the substitution does not seem to be working. It seems like an easy problem to fix, but I'm not gettting anywhere with it. Thank you.

Replies are listed 'Best First'.
Re: replacing blank lines
by OeufMayo (Curate) on Mar 06, 2001 at 01:28 UTC

    Hello,
    If the only thing you want to do is to weed out the blank lines, a one-liner could do the trick:

    *nix version: perl -ni.bak -e 'print unless /^\s*$/' myFile Win32: perl -ni.bak -e "print unless /^\s*$/" myFile

    In the future, you may want to check the search functionnality of perlmonks, I found all these relevant nodes about your question:

    Cheers,
    Briac

    Update:
    For this node to be really useful here's why your snippet doesn't work:

    Your regex did indeed match an empty line, but the newline "\n" character is not matched by the regex. So your program happily replace an empty string (with a non-matched newline) with another empty string (with the newline it has not matched). Should you have chomp'd the line, everything would have run smoothly (but you'd then have to add the newline at the end of the lines you want to print)

    <kbd>--
    my $OeufMayo = new PerlMonger::Paris({http => 'paris.mongueurs.net'});</kbd>
Re: replacing blank lines
by Tuna (Friar) on Mar 06, 2001 at 01:54 UTC
    one way is:
    #!/usr/bin/perl -w use strict; my $line; my $element; my @list; while ($line = <DATA>){ chomp $line; unless ($line =~ /^\s*$/) { push(@list, $line); } } foreach $element(@list){ print "my remaining lines are: $element\n"; } __DATA__ 1 1 Foo Bar Baz 2 2 Foo Bar Baz __END__
Re: replacing blank lines
by Anonymous Monk on Mar 06, 2001 at 04:46 UTC
    The problem is you're reading the file one line at a time. If you find a blank line, you're essentially replacing it with another blank line, so there's no net change. If you process it one line at a time, you need to write nonblank lines out to a separate file as you go, so it will have the nonblanks when you're done. If you know the file won't be too big, I think you could also use this regex:
    $entire_file =~ s/^\n$//mg;
    That should leave $entire_file with only nonblank lines. HTH.
      $entire_file =~ s/^\n$//mg;

      That should leave $entire_file with only nonblank lines

      This is assuming that blank lines are TRUELY blank (no spaces). This is more general:

      perl -pi -e 's!^\s+?$!!' file.txt or s!^(\n|\s+)$!!g

      Cheers,
      KM

        The first one-liner will not have the intended effect:     perl -pi -e 's,^\s+?$,,' file.txt
        only works on lines that consist of a newline. For lines that contain whitespace followed by a newline, the newline is not removed because of the non-greedy quantifier. (Although you could run the one-liner twice... ;)

        In the second one-liner:     perl -pi -e 's,^(\n|\s+)$,,g' file.txt
        the alternation and the /g are unnecessary.

        This is sufficient: perl -pi -e 's,^\s+$,,' file.txt

Re: replacing blank lines
by Fingo (Monk) on Mar 06, 2001 at 01:49 UTC
    This might be a roundabout way of doing it:
    unless (<FILE> =~ /([a-z]|[A-Z]|[0-9]/) { #delete line }


    Thus Spake the Master Programmer:
    "After three days without programming, life becomes meaningless."
         ---Tao of Programing
Re: replacing blank lines
by kleinbiker7 (Sexton) on Mar 06, 2001 at 05:27 UTC
    Try something like this.
    #!/usr/local/bin/perl open (FILE, "/home/robert/Print/foo") || die "Cant open /home/robert/f +oo\n\n"; # Create a file array of lines while ($line = <FILE>) { push (@array, $line); } # Then control the output foreach $index (@array) { # if the line is completely blank dont print it. if ($index !~ /^\s*\n\s*$/) { print "$index"; } }
      # Create a file array of lines while ($line = <FILE>){ push (@array, $line); }

      ew.... just do:

      my @array = <FILE>;

      # Then control the output foreach $index (@array){ # if the line is completely blank dont print it. if ($index !~ /^\s*\n\s*$/) { print "$index"; } }

      ewwwww... use something more like:

      for (@array) { print unless /^(\n|\s+)$/; }

      Even better, do it from the command line (see another node in this same thread)

      Cheers,
      KM

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2019-12-09 03:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?