需要复杂的grep awk命令

时间:2015-08-13 05:50:18

标签: shell unix awk grep

我有loge文件有很多行,下面的行是它的样本 我有2把钥匙。第一个关键是sub。 Id(1112222222)第二个密钥是会话ID(xxx.apn.com; 2418561818; 846; 60034)。

sub Id已知,我需要创建一个大的shell命令来搜索该子id的会话ID,然后在文件中搜索具有其中一个键或两者的所有行。例如,下面的行包含会话ID和子ID对。

úú 3 [ +548] EPC_Log: bi_tspSequenceId=1; i_tspSeverity=6;  vc_MessageInformation=CCR received. SessionId: xxx.apn.com;2418561818;846;60034, SubsId: 1112222222, IpAddr: 1.1.1.1, Protocol: Gx, RequestType: Create, APN: apn.com, Event-Trigger: ;i_Priority=6úúûû TelORB: Tue Aug 11 12:26:05 2015,  Host:úú Tue Aug 11 12:26:18 2015

输入前

úú 3 [ +548] EPC_Log: bi_tspSequenceId=1; i_tspSeverity=6;  vc_MessageInformation=CCR received. SessionId: xxx.apn.com;2418561818;846;60034, SubsId: 1112222222, IpAddr: 1.1.1.1, Protocol: Gx, RequestType: Create, APN: apn.com, Event-Trigger: ;i_Priority=6úúûû TelORB: Tue Aug 11 12:26:05 2015,  Host:úú Tue Aug 11 12:26:18 2015
úú 3 [ +548] EPC_Log: bi_tspSequenceId=1; i_tspSeverity=6;  vc_MessageInformation=CCA received. SessionId: xxx.apn.com;2418561818;846;60034, SubsId: 1112222222, IpAddr: 1.1.1.1, Protocol: Gx, RequestType: Create, APN: apn.com, Event-Trigger: ;i_Priority=6úúûû TelORB: Tue Aug 11 12:26:05 2015,  Host:úú Tue Aug 11 12:26:18 2015
úú 3 [ +548] EPC_Log: bi_tspSequenceId=1; i_tspSeverity=6;  vc_MessageInformation=CCR received. SessionId: yyy.apn.com;2418561818;846;60034, SubsId: 1113333333, IpAddr: 1.1.1.1, Protocol: Gx, RequestType: Create, APN: apn.com, Event-Trigger: ;i_Priority=6úúûû TelORB: Tue Aug 11 12:26:05 2015,  Host:úú Tue Aug 11 12:26:18 2015
úú 3 [ +548] EPC_Log: bi_tspSequenceId=1; i_tspSeverity=6;  vc_MessageInformation=CCA received. SessionId: yyy.apn.com;2418561818;846;60034, SubsId: 1112222222, IpAddr: 1.1.1.1, Protocol: Gx, RequestType: Create, APN: apn.com, Event-Trigger: ;i_Priority=6úúûû TelORB: Tue Aug 11 12:26:05 2015,  Host:úú Tue Aug 11 12:26:18 2015

输出例如

úú 3 [ +548] EPC_Log: bi_tspSequenceId=1; i_tspSeverity=6;  vc_MessageInformation=CCR received. SessionId: xxx.apn.com;2418561818;846;60034, SubsId: 1112222222, IpAddr: 1.1.1.1, Protocol: Gx, RequestType: Create, APN: apn.com, Event-Trigger: ;i_Priority=6úúûû TelORB: Tue Aug 11 12:26:05 2015,  Host:úú Tue Aug 11 12:26:18 2015
úú 3 [ +548] EPC_Log: bi_tspSequenceId=1; i_tspSeverity=6;  vc_MessageInformation=CCA received. SessionId: xxx.apn.com;2418561818;846;60034, SubsId: 1112222222, IpAddr: 1.1.1.1, Protocol: Gx, RequestType: Create, APN: apn.com, Event-Trigger: ;i_Priority=6úúûû TelORB: Tue Aug 11 12:26:05 2015,  Host:úú Tue Aug 11 12:26:18 2015
úú 3 [ +548] EPC_Log: bi_tspSequenceId=1; i_tspSeverity=6;  vc_MessageInformation=CCA received. SessionId: yyy.apn.com;2418561818;846;60034, SubsId: 1112222222, IpAddr: 1.1.1.1, Protocol: Gx, RequestType: Create, APN: apn.com, Event-Trigger: ;i_Priority=6úúûû TelORB: Tue Aug 11 12:26:05 2015,  Host:úú Tue Aug 11 12:26:18 2015

2 个答案:

答案 0 :(得分:0)

未测试

提取给定子ID的会话ID:

subsId=1112222222
sessionIds=$( grep -oP 'SessionId: [^,]+,( SubsId: $subsId,)' file.log | sort -u)

然后使用其中一个会话ID提取行:

echo "$sessionIds" | grep -Ff- file.log

答案 1 :(得分:0)

这将执行您想要的操作,但它不会重现已发布的预期输出,因为这不会反映您所说的内容*

$ cat tst.awk
BEGIN { subsid = subsid"," }
NR==FNR { if ($13 == subsid) sessids[$11]; next }
($11 in sessids) || ($13 == subsid)

$ awk -v subsid=1112222222 -f tst.awk file file
úú 3 [ +548] EPC_Log: bi_tspSequenceId=1; i_tspSeverity=6;  vc_MessageInformation=CCR received. SessionId: xxx.apn.com;2418561818;846;60034, SubsId: 1112222222, IpAddr: 1.1.1.1, Protocol: Gx, RequestType: Create, APN: apn.com, Event-Trigger: ;i_Priority=6úúûû TelORB: Tue Aug 11 12:26:05 2015,  Host:úú Tue Aug 11 12:26:18 2015
úú 3 [ +548] EPC_Log: bi_tspSequenceId=1; i_tspSeverity=6;  vc_MessageInformation=CCA received. SessionId: xxx.apn.com;2418561818;846;60034, SubsId: 1112222222, IpAddr: 1.1.1.1, Protocol: Gx, RequestType: Create, APN: apn.com, Event-Trigger: ;i_Priority=6úúûû TelORB: Tue Aug 11 12:26:05 2015,  Host:úú Tue Aug 11 12:26:18 2015
úú 3 [ +548] EPC_Log: bi_tspSequenceId=1; i_tspSeverity=6;  vc_MessageInformation=CCR received. SessionId: yyy.apn.com;2418561818;846;60034, SubsId: 1113333333, IpAddr: 1.1.1.1, Protocol: Gx, RequestType: Create, APN: apn.com, Event-Trigger: ;i_Priority=6úúûû TelORB: Tue Aug 11 12:26:05 2015,  Host:úú Tue Aug 11 12:26:18 2015
úú 3 [ +548] EPC_Log: bi_tspSequenceId=1; i_tspSeverity=6;  vc_MessageInformation=CCA received. SessionId: yyy.apn.com;2418561818;846;60034, SubsId: 1112222222, IpAddr: 1.1.1.1, Protocol: Gx, RequestType: Create, APN: apn.com, Event-Trigger: ;i_Priority=6úúûû TelORB: Tue Aug 11 12:26:05 2015,  Host:úú Tue Aug 11 12:26:18 2015

*为什么在您的问题中显示的输出中不是所有4个输入行?

您的目标SubsId 1112222222与第1行和第2行的SessionId xxx.apn.com;2418561818;846;60034以及第4行的SessionId yyy.apn.com;2418561818;846;60034相关联。

您说要输出all lines which have one of those keys or both of them。与第4行一样,第3行有SessionId yyy.apn.com;2418561818;846;60034

因此,由于匹配SubsId 1112222222而应打印第1,2和4行,并且应打印第3行以匹配SessionId yyy.apn.com;2418561818;846;60034,因为SubsId 1112222222与该SessionId相关联第4行。

右?