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

时间:2013-03-06 05:47:42

标签: awk

输入 - file1.txt

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

注意:

  1. 查找已发送及其即时接收 - 仅限匹配会话编号
  2. 将所有会话匹配“接收”与“已发送”相同。例如:session:123
  3. 如果没有“收到”,则打印NOMATCH

2 个答案:

答案 0 :(得分:2)

试试这个:

awk -F "[:,]"  '{split($2,a,",");if($1~/sent/){if(prev!~/receive/&& NR!=1)print prev,"NOMATCH";else if (NR!=1)print prev;prev=$0;session=a[1];}if($1~/receive/&&session==a[1]){prev=prev","$0;}}'END'{if(prev~/receive/)print prev;}' file1.txt

答案 1 :(得分:1)

awk '{x=substr($3,0,match($3,","));a[x]?a[x]=a[x]","$0:a[x]=$0}END{for(i in a){print a[i],a[i]~/sent/&&a[i]~/receive/?"":"NOMATCH"}}' your_file

测试:

>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
pqr message=receive session:123,4x,5x,7x
pqr message=receive session:123,4p,5q,7r
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
> 
> 
> 
> awk '{x=substr($3,0,match($3,","));a[x]?a[x]=a[x]","$0:a[x]=$0}END{for(i in a){print a[i],a[i]~/sent/&&a[i]~/receive/?"":"NOMATCH"}}' 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,pqr message=receive session:123,4x,5x,7x,pqr message=receive session:123,4p,5q,7r 
abc message=receive session:311,x,y,z NOMATCH
abc message=sent session:589,x,y,z,pqr message=receive session:589,4,5,7 
abc message=sent session:342,x,y,z NOMATCH
>