Perl: Match a line with multiple search patterns

Hi

I'm not very good with the serach patterns and I'd need a sample how to find a line that has multiple patterns.

Say I want to find a line that has "abd", "123" and "QWERTY" and there can be any characters or numbers between the serach patterns, I have a file that has thousands of lines and each of them can contain one or two of above patterns.

What I want is to print out a line that has all those 3 search patterns.

Thanks,
//Juha

awk '/123/ && /abd/ && /QWERTY/{print}'  file

It is not clear if you need to check for all the patterns or any of the patterns:

All of them:

if (/123/ && /abd/ && /QWERTY/) {
    found all patterns
}

any of them:

if (/123/ ||  /abd/ || /QWERTY/) {
    found one or more patterns
}

Thanks danmero and KevinADC! :b: I took the example from KevinADC as I'm trying to get on top of perl scripting :slight_smile:

Look like the OP ask for all patterns on the same line

Yes, but the next sentence makes that ambiguous (to me anyway):

ok.. Let me clarify a little:

Originally I wanted to print a line only if ALL the patterns can be found from that line, so this was ok:

if (/123/ && /abd/ && /QWERTY/) {
    found all patterns
}

Now, I found a different scenario, which I've tried to figure out... but have to admit that could not..

I'd like to print the line only if I can find "123" and abc", but not "efg":

I thought maybe I could use:

if (/123/ && /abc/ !& /efg/) {
    found all patterns
}

But !& does not seem to be a valid operator... What would be a way around it?

Thanks.. and sorry for being a bit unclear..

Use plain english: &&(and) !(not).

if (/123/ && /abc/ &&! /efg/) {
    found all patterns
}

I think perl will interpret that correctly but it is written more clearly with a space between && and !

if (/123/ && /abc/ && !/efg/) {
    found all patterns
}

Thanks again. :slight_smile: Slowly learning....

I also came up with something that seems to work...

if ( $_ =~ /123/ && $_ =~ /abc/ && $_ !~ /efg/) {
   print "Line has 123 and abc, and not efg";
}

That is the exact same thing written in long form. Sort of like "do not" and "don't", written differently but have the same meaning.