awk match search patterns and print immediate next line's 4th & 5th values

265 views Asked by At

I want to match the pattern of 4th and 5th value if they matches print Nextline's 4th and 5th value.

Below is the input file

cat

CL3-A3-2  0    0 17496  2851 P-VOL PAIR ASYNC      0  2850 -
CL3-C2-5  0    0 66319  2850 S-VOL PAIR ASYNC      0  2851 -
CL3-A3-2  0    1 17496  2852 P-VOL PAIR ASYNC      0  2851 -
CL3-C2-5  0    1 66319  2851 S-VOL PAIR ASYNC      0  2852 -
CL3-A3-2  0    2 17496  2853 P-VOL PAIR ASYNC      0  2852 -
CL3-C2-5  0    2 66319  2852 S-VOL PAIR ASYNC      0  2853 -
CL3-A3-2  0    6 17496  2857 P-VOL PAIR ASYNC      0  2857 -
CL3-C2-5  0    3 66319  2857 S-VOL PAIR ASYNC      0  2857 -
CL3-A3-2  0    6 47496  2857 P-VOL PAIR ASYNC      0  2857 -
CL3-C2-5  0    3 18496  2857 S-VOL PAIR ASYNC      0  2857 -

For Ex: I'm matching 17496 and 2857, If the search pattern matches in a line, Need to get the immediate nextline's 4th and 5th value using awk or sed is fine.

output would be like

66319 2857

Which reduce my duplication while matching pattern

Thanks

4

There are 4 answers

0
skmohan On

I just fixed it with below line

nawk '/'$4'  '$5'/{getline;print $4,$5}'
0
Rafe On

This is straightforward:

lastMatched {
    print $4, $5;
    lastMatched = 0;
}

$4 == 17496 && $5 == 2857 {
    lastMatched = 1;
}
0
Ajay A On
awk '{if (flag==1) {print $4,$5; flag=0}}
     {if (($4==17496)&&($5==2857))flag=1}' inputfile

If you need only one match and want to terminate afterwards simply add an exit.

awk '{if (flag==1) {print $4,$5; exit}}
     {if (($4==17496)&&($5==2857)) flag=1}' inputfile 
0
gboffi On

If I see a match I raise a flag, if the flag is raised I lower the flag and print the data.

% awk 'p==1{p=0;print $4, $5} $4==17496&&$5==2857{p=1}' your.data
66319 2857
%

The order of the statements is crucial, if I'd first set the flag and then check it, I'd print $4 and $5 for the same line...