Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

trim the blank lines...regex

by dani_cv (Acolyte)
on Apr 11, 2008 at 18:48 UTC ( #679815=perlquestion: print w/ replies, xml ) Need Help??
dani_cv has asked for the wisdom of the Perl Monks concerning the following question:

Can any one please help me to remove the blank lines if present as follows
$str = "bla bla bla :

==bla bla

==bla bla


====bla bla bla


....

....

";
I need this string to be as :
$str = "bla bla bla :
==bla bla
==bla bla
====bla bla bla
.... ...... ";

Comment on trim the blank lines...regex
Re: trim the blank lines...regex
by FunkyMonk (Canon) on Apr 11, 2008 at 18:54 UTC
Re: trim the blank lines...regex
by kyle (Abbot) on Apr 11, 2008 at 18:58 UTC
    my $str = "bla bla bla : ==bla bla ==bla bla ====bla bla bla .... .... "; $str =~ s/\n\n+/\n/g; print qq{\$str = "$str";\n}; __END__ $str = "bla bla bla : ==bla bla ==bla bla ====bla bla bla .... .... ";
      Thanks a lot...works perfect!.
      Dan.
Re: trim the blank lines...regex
by apl (Monsignor) on Apr 11, 2008 at 19:00 UTC
    You're asking about the core of Perl, regular expressions. What have you tried so far?

    Have you asked your teacher for additional help?

Re: trim the blank lines...regex
by ww (Bishop) on Apr 11, 2008 at 19:00 UTC
Re: trim the blank lines...regex
by pc88mxer (Vicar) on Apr 11, 2008 at 19:06 UTC
    $str =~ s/^\s*\n//mg; or $str =~ s/^\n//mg depending on your definition of a blank line.

    Perhaps a hangup here is that s/^$// will match a blank line but will not remove the ending newline since $ doesn't include the newline.

Re: trim the blank lines...regex
by wade (Pilgrim) on Apr 11, 2008 at 19:37 UTC

    I'm a little afraid of answering this 'cause it looks a bit like homework. That said...

    The first question I have is "how did you get the string in the first place?" The answer to this might present a better solution (e.g., don't concatenate blank lines to $str in the firs place).

    To answer your original question, though:

    use strict; use warnings; use diagnostics; print "START: '$str'"; $str =~ s/\n\n+/\n/g; print "\nEND: '$str'";
    --
    Wade
Re: trim the blank lines...regex
by Anonymous Monk on Apr 11, 2008 at 20:10 UTC
    Have you independently confirmed that these really are multiple newlines? Perhaps each line starts with "==bla bla" but is then followed by hundreds of blanks, so your editor is simply line wrapping until it finds a newline char? Some editors (e.g. vi) would display such lines very much as you describe. It's always worthwhile to start with the obvious.
Re: trim the blank lines...regex
by sundialsvc4 (Abbot) on Apr 11, 2008 at 21:24 UTC

    In an arch-typical file-processing loop (such as the ones described in, ummm, the multitudinous perlfaqs...), the usual procedure is to use chomp to remove any newlines, then use a regular-expression to eliminate unwanted lines. Then output the ones that pass, remembering to include the record-separator.

    For such a trivial task as this, the grep command-line tool might already be exactly what you need.

    Otherwise, your regular expression is probably going to be something like: /^\s*$/ which breaks-down as follows:

    • The "^" character marks the beginning of the line, and the "$" marks the end. These are used to bind the enclosing terms to “the entire record,” so that you are searching for records which consist exclusively of what's between.
    • The \s is a character-class reference:   anything that qualifies as “white space.”
    • Finally, the "*" means “zero-or-more of the foregoing.”
    • So this regular-expression will match a record that consists entirely of zero-or-more whitespace.

    As I previously mentioned, you can probably do the entire job with the grep command, using the "-v" modifier to invert the meaning of the pattern:   all lines which do not match the pattern will be output. This omits the blank lines (which are the ones that match) and outputs the non-blanks. Q.E.D.

    (An equivalent Perl program could of-course be constructed, but since this is trivial ...)

Re: trim the blank lines...regex
by mr_mischief (Monsignor) on Apr 11, 2008 at 23:23 UTC
    You've already been given good advice, including RTFM which really is good advice here. Yet in the spirit of TIMDOWTDI (some of which don't involve regexes)...
    while ( <> ) { chomp; print "$_\n" if length $_; }
    { local $/; my $contents = <>; $contents =~ tr/\n/\n/s; print $contents; }
    If you're playing golf:
    $/='';chomp,print"$_\n"while<>
    print/./?$_:''while<>
    print/./?$_:''for<>
    If you're using one-liners:
    perl -ne "print if/./"
    perl -0pe "y/\n/\n/s"
    perl -ne "/./&&print"

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2014-10-22 04:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (112 votes), past polls