Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Shell 需要复杂的grep-awk命令_Shell_Unix_Awk_Grep - Fatal编程技术网

Shell 需要复杂的grep-awk命令

Shell 需要复杂的grep-awk命令,shell,unix,awk,grep,Shell,Unix,Awk,Grep,我有日志文件有很多行,下面的一行是它的样本 我有两把钥匙。第一个密钥是sub.Id(1112222222),第二个密钥是会话Id(xxx.apn.com;2418561818;846;60034) 如果知道子Id,我需要创建一个大shell命令来搜索这个子Id的会话Id,然后在文件中搜索所有包含其中一个或两个键的行。例如,下一行包含会话id和子id对 úú 3 [ +548] EPC_Log: bi_tspSequenceId=1; i_tspSeverity=6; vc_MessageInf

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

如果知道子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
输出示例

úú 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
未经测试

要提取给定SUB id的会话id,请执行以下操作:

subsId=1112222222
sessionIds=$( grep -oP 'SessionId: [^,]+,( SubsId: $subsId,)' file.log | sort -u)
然后,要提取具有其中一个会话ID的行:

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

这将实现您所说的目标,但不会再现发布的预期输出,因为这并不反映您所说的目标
*

$ 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行输入

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

你说你想输出所有有一个或两个键的行。与第4行一样,第3行也有SessionId
yyy.apn.com;2418561818;846;60034

因此,第1行、第2行和第4行应该打印,因为匹配了SUFFID
11122222
,第3行应该打印,因为匹配了SessionId
yyy.apn.com;2418561818;846;60034
因为SUFFID
1112222222
与第4行的SessionId关联


是吗?

请提供样本输入数据和相应的输出,您可以在更新样本后在正文中查看。首先感谢您的帮助。第3行不应匹配,因为sessionID yyy与sessionID xxx不同。我正在筛选具有子ID 1112222222及其会话ID xxx的行,但会话ID yyy与子ID 11133333333相关,但子ID 1112222222与会话ID yyy在第
SessionId:yyy行关联。。。补贴:1112222222
。为了选择要输出的行,您如何决定补贴1112222222只应与
xxx
关联,而不与
yyy
关联?您是对的。考虑最后一个是不同的子ID