sdf
June 30, 2011, 4:07pm
1
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/'
sdf
June 30, 2011, 4:16pm
3
neutronscott:
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?
sdf
June 30, 2011, 4:59pm
6
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
sdf
July 1, 2011, 3:29am
8
shamrock:
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
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
sdf
July 1, 2011, 3:39am
10
Now i got it. Since i use gawk with DOS:wall: i need to add a backslash.
pat=pat2=\"\"
sdf
July 2, 2011, 4:03am
11
shamrock:
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
@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