输入 - 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
注意:
答案 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
>