awk
'{
split($3,a,",");
if($2~/sent/)
{
if(prev!~/receive/ && NR!=1)
print prev,"NO MATCH";prev=$0;session=a[1];
}
if($2~/receive/&&session==a[1])
{
print session, prev,$0;prev=$0
}
}' temp
测试:
> cat temp
abc message=sent session:111,x,y,z
pqr message=receive session:111,4,5,7
abc message=sent session:123,x,y,z
pqr message=receive session:123,4,5,7
abc message=sent session:342,x,y,z
abc message=sent session:589,x,y,z
pqr message=receive session:589,4,5,7
> awk '{split($3,a,",");if($2~/sent/){if(prev!~/receive/&& NR!=1)print prev,"NO MATCH";prev=$0;session=a[1];}if($2~/receive/&&session==a[1]){print session, prev,$0;prev=$0}}' temp
session:111 abc message=sent session:111,x,y,z pqr message=receive session:111,4,5,7
session:123 abc message=sent session:123,x,y,z pqr message=receive session:123,4,5,7
abc message=sent session:342,x,y,z NO MATCH
session:589 abc message=sent session:589,x,y,z pqr message=receive session:589,4,5,7
>
或者您可以简单地将代码放在一个文件中,如下所示:
> cat tmp.awk
#!/usr/bin/awk
{
split($3,a,",");
if($2~/sent/)
{
if(prev!~/receive/ && NR!=1)
print prev,"NO MATCH";prev=$0;session=a[1];
}
if($2~/receive/&&session==a[1])
{
print session, prev,$0;prev=$0
}
}
>
并执行如下:
> awk -f tmp.awk temp
session:111 abc message=sent session:111,x,y,z pqr message=receive session:111,4,5,7
session:123 abc message=sent session:123,x,y,z pqr message=receive session:123,4,5,7
abc message=sent session:342,x,y,z NO MATCH
session:589 abc message=sent session:589,x,y,z pqr message=receive session:589,4,5,7
>