1

输入 - 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. 查找已发送及其立即接收 - 仅适用于 MATCHING 会话号
  2. 将所有会话匹配“接收”与相同的“发送”结合起来。例如:会话:123
  3. 如果没有“接收”,则打印 NOMATCH
4

2 回答 2

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
于 2013-03-06T05:50:00.747 回答
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
> 
于 2013-03-06T06:14:06.580 回答