合并场匹配的基础上线

合并场匹配的基础上线

本文介绍了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=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

注:

如果您在源文件中看到,对于每一个发送的消息有接收

仅用于会话= 342有没有收到

会议是未知,不能硬codeD

因此,合并只有那些收发,我们有匹配的会话数

If you see in source file, for every "sent" message there is "receive"
only for session=342 there is no receive
session is unknow, can't be hardcoded
So merge only those sent and receive where we have matching session number

推荐答案

另一种方式:

awk -F "[:,]"  '/=sent/{a[$2]=$0;}/=receive/{print a[$2], $0;delete a[$2];}END{for(i in a)print a[i],"NO MATCH";}' file

结果:

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

在该遇到送记录,它是与会话ID作为索引在数组中存储。当接收遇到战绩,发送记录是从阵列中取出,并沿印刷接收记录。此外,发送记录从数组作为当接收记录已接删除。最后,阵列中所有剩余的记录打印为不匹配。

When the send record is encountered, it is store in the array with the session id as the index. When the receive record is encountered, the send record is fetched from the array and printed along with receive record. Also, sent records are removed from array as and when receive records are received. At the END, all the remaining records in the array are printed as NO MATCH.

这篇关于AWK |合并场匹配的基础上线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 20:08