-1

我需要以下帮助:

输入文件:

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=receive session:311,x,y,z
abc message=sent session:589,x,y,z
pqr message=receive session:589,4,5,7

输出文件:

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, NOMATCH
abc message=sent session:589,x,y,z, pqr message=receive session:589,4,5,7

笔记:

如果您在源文件中看到,对于每个“已发送”消息,都有“接收”对于每个“=发送”,在立即
下一行 中搜索“=接收”仅针对相同的会话号 所以只合并那些发送和接收的我们有的地方匹配会话号 ELSE 按顺序打印发送, 仅用于会话 = 342 没有接收 会话是未知的,不能被硬编码



4

1 回答 1

0
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
> 
于 2013-02-14T12:46:05.257 回答