3

我需要以下帮助:

输入文件:

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

笔记:

如果您在源文件中看到,对于每个“已发送”消息,“接收”
仅适用于 session=342 没有接收
会话是未知的,不能被硬编码
所以只合并那些发送和接收的我们有匹配的会话号

4

2 回答 2

1

这是使用awk. 像这样运行:

awk -f script.awk file

内容script.awk

{
    x = $0

    gsub(/[^:]*:|,.*/,"")

    a[$0] = (a[$0] ? a[$0] "," FS : "") x
    b[$0]++
}

END {
    for (i in a) {
        print (b[i] == 2 ? a[i] : a[i] "," FS "NOMATCH") | "sort"
    }
}

结果:

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

或者,这是单线:

awk '{ x = $0; gsub(/[^:]*:|,.*/,""); a[$0] = (a[$0] ? a[$0] "," FS : "") x; b[$0]++ } END { for (i in a) print (b[i] == 2 ? a[i] : a[i] "," FS "NOMATCH") | "sort" }' file

请注意,sort如果您不关心排序输出,则可以将管道放到。HTH。

于 2013-02-13T06:11:17.283 回答
1

其他方式:

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

send遇到记录时,它以会话 id 作为索引存储在数组中。当receive遇到记录时,send从数组中取出记录并与receive记录一起打印。此外,发送的记录会在收到记录时从数组中删除receive。在 END 处,数组中的所有剩余记录都打印为 NO MATCH。

于 2013-02-13T06:29:50.647 回答