Solved

Multi pattern match

Posted on 2001-07-31
5
599 Views
Last Modified: 2008-02-01
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
Comment
Question by:tel2
  • 3
  • 2
5 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 6339110
print grep /abc|def|ghi|jkl/ <>;
0
 
LVL 84

Expert Comment

by:ozo
ID: 6339119
print grep /abc|def|ghi|jkl/,<>;
0
 
LVL 12

Author Comment

by:tel2
ID: 6343456
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
 
LVL 84

Accepted Solution

by:
ozo earned 30 total points
ID: 6345384
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
 
LVL 12

Author Comment

by:tel2
ID: 6374742
ozo,

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

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
perl rename 2 142
Extract multiple value with delimiters from a string 4 170
Perl DBI Transactions Using Custom Module 7 47
PERL - Find newest folder 12 133
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

805 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question