How to filter out lines from a variable that has multi-lines?

Discussion in 'Perl Misc' started by mike, Sep 29, 2007.

  1. mike

    mike Guest

    suppose a variable $a has multi-lines, e.g.

    happy
    new
    new password
    year
    get password of micky
    LOL

    What is best way to filter out lines that have "password" involved?
    Thanks in advance
     
    mike, Sep 29, 2007
    #1
    1. Advertising

  2. mike

    Narthring Guest

    On Sep 28, 8:09 pm, mike <> wrote:
    > suppose a variable $a has multi-lines, e.g.
    >
    > happy
    > new
    > new password
    > year
    > get password of micky
    > LOL
    >
    > What is best way to filter out lines that have "password" involved?
    > Thanks in advance


    One way would be to split on newlines and use a regular expression to
    filter out 'password':


    use strict;
    use warnings;

    my $text = '
    happy
    new
    new password
    year
    get password of micky
    LOL';


    my (@array) = split(/\n/, $text);

    my ($result);
    foreach my $line (@array){
    $result .= "$line\n" unless ($line =~ m/password/);
    }

    print "$result";
     
    Narthring, Sep 29, 2007
    #2
    1. Advertising

  3. mike <> wrote:
    > suppose a variable $a has multi-lines, e.g.
    >
    > happy
    > new
    > new password
    > year
    > get password of micky
    > LOL
    >
    > What is best way to filter out lines that have "password" involved?



    $a =~ s/.*password.*\n//g;


    --
    Tad McClellan
    email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
     
    Tad McClellan, Sep 29, 2007
    #3
  4. mike

    mike Guest

    both good advices.
    Thanks for all you guys replies.
     
    mike, Oct 2, 2007
    #4
    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. zax75
    Replies:
    1
    Views:
    1,142
  2. chiefsss
    Replies:
    0
    Views:
    510
    chiefsss
    Aug 21, 2009
  3. Ben Burch
    Replies:
    23
    Views:
    420
    Eric Pozharski
    Sep 2, 2009
  4. PerlFAQ Server
    Replies:
    0
    Views:
    180
    PerlFAQ Server
    Jan 14, 2011
  5. PerlFAQ Server
    Replies:
    0
    Views:
    168
    PerlFAQ Server
    Apr 19, 2011
Loading...

Share This Page