Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 621
  • Last Modified:

Multi pattern match

UNIX has a "-E" switch to allow its grep utility to match multiple patterns at once, eg:
  grep -E "abc|def|ghi|jkl" infile
will match all lines in infile which contain "abc" or "def", etc.

I know Perl has a "grep" command, and I know it can also do pattern matching with the "=~" operator, but how can I write CONCISE Perl code to match multiple patterns with the:
1. grep command?
2. "=~" operator?

Thanks.
0
tel2
Asked:
tel2
  • 3
  • 2
1 Solution
 
ozoCommented:
print grep /abc|def|ghi|jkl/ <>;
0
 
ozoCommented:
print grep /abc|def|ghi|jkl/,<>;
0
 
tel2Author Commented:
ozo,

Is this an answer to both questions, or just #1?  If just #1, are there any other commands (eg: "=~") which allow for multiple pattern matching, if so, please give examples.

What does the "<>" mean exactly?
0
 
ozoCommented:
while( defined($strimg = <>) ){
    print $string if $string =~ /abc|def|ghi|jkl/;
}


perldoc -f readline
                 readline EXPR
                         Reads   from  the  filehandle  whose  typeglob  is
                         contained in EXPR.  In scalar context,  each  call
                         reads and returns the next line, until end-of-file
                         is reached, whereupon the subsequent call  returns
                         undef.   In  list context, reads until end-of-file
                         is reached and returns a list of lines.  Note that
                         the  notion  of  ``line'' used here is however you
                         may    have    defined    it    with     $/     or
                         $INPUT_RECORD_SEPARATOR).  See the section on "$/"
                         in the perlvar manpage.
                         When $/ is set to undef,  when  readline()  is  in
                         scalar context (i.e. file slurp mode), and when an
                         empty file is read, it returns '' the first  time,
                         followed by undef subsequently.
                         This  is  the  internal  function implementing the
                         <EXPR> operator, but you can use it directly.  The
                         <EXPR> operator is discussed in more detail in the
                         I/O Operators entry in the perlop manpage.
                             $line = <STDIN>;
                             $line = readline(*STDIN);           # same thing

0
 
tel2Author Commented:
ozo,

Thanks for that.
So, are you saying that "<>" is the same as "readline"?
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now