awk: Multiple search patterns & print in an one liner

I would like to print result of multiple search pattern invoked from an one liner. The code looks like this but won't work

gawk -F[.:] '{{if ($0 ~ /pattern1/) pat1=$1 && if ($0 ~ /pattern2/) pat2=$2} ; print pat1, pat2}'

Can anybody help getting the right code?

Print the line or the pattern?

This will print only lines containing pattern1 and pattern2

awk '/pattern1/ && /pattern2/'

Tanks neutronscott.

Got this far too, but pattern1 is in a different line than pattern2 and i would like to print $1 of line $0 found with pattern1 and $2 of line $0 found with pattern2

gawk -F[.:] '{if ($0 ~ /pattern1/) pat1=$1; if ($0 ~ /pattern2/) pat2=$2}{if (pat1 && pat2) print pat1, pat2}' file

So it's just one match per file?

Yes this works! Thanks! Though it prints some lines multiple the same time. Like this:

4827  235,-- EUR
4827  235,-- EUR
4830  235,-- EUR
4830  235,-- EUR
4830  235,-- EUR
4830  383,-- EUR

Depending on the number of lines in a file.

yeah i should have caught that mistake so here's the updated version...

gawk -F[.:] '{if ($0 ~ /pattern1/) pat1=$1; if ($0 ~ /pattern2/) pat2=$2}{if (pat1 && pat2) print pat1, pat2;pat=pat2=""}' file
1 Like

Tried this code:

pat=pat2=""}'

and

pat=pat1=""}'

Though, both return an empty string.

Thought this multiple search pattern in an one liner is easier.

Try this

 
gawk -F[.:] '{if ($0 ~ /pattern1/) pat1=$1; if ($0 ~ /pattern2/) pat2=$2}{if (pat1 && pat2) {print pat1, pat2;pat1=pat2=""}}' file

Now i got it. Since i use gawk with DOS:wall: i need to add a backslash.

pat=pat2=\"\" 

@shamrock

again need to ask, got the desired results, though if field pat2 returns the digit=0 the print of the line and field will be ommited. How can the code be changed, so that pat2 will be printed with the digit=0?

desired output:

pat1 pat2
4830 0
4831 250
4832 0