awk |在字段匹配的基础上合并文件中的连续2行

时间:2013-02-14 12:16:41

标签: awk

我需要以下帮助:

输入文件:

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

注意:

如果您在源文件中看到,对于每个“已发送”消息,都有“接收”
对于每个“=已发送”,仅在立即 NEXT LINE中搜索“= receive”以获取相同的会话号 因此只合并那些发送和接收的地方,我们有匹配的会话号ELSE打印发送的顺序为
仅对于session = 342,没有收到
会话是未知的,不能硬编码

1 个答案:

答案 0 :(得分: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
>