使用 comb 过滤多个字段

假设有如下数据:

vcpnode.log.2018-12-12.0-[10.0.35.120]:2018-12-12/08:28:41.826|10.0.35.120|api-vcp.vipkid.com.cn-17874-4837116408-0-1544574521.795|0|^_^|[http-nio-8080-exec-730] INFO  com.vipkid.vcpnode.controller.IdcNodeController 51 - IdcNodeController.compareIP false -> clientIP=101.241.195.137, req=IdcNodeReq(userId=9025394, role=STUDENT, supplierCode=6, ip=219.143.155.44, vendor=null, lineCode=null), classId=158912874
vcpnode.log.2018-12-12.0-[10.0.35.120]:2018-12-12/09:15:11.583|10.0.35.120|api-vcp.vipkid.com.cn-63650-4774716278-0-1544577311.550|0|^_^|[http-nio-8080-exec-710] INFO  com.vipkid.vcpnode.controller.IdcNodeController 51 - IdcNodeController.compareIP false -> clientIP=112.0.107.254, req=IdcNodeReq(userId=27186676, role=STUDENT, supplierCode=null, ip=218.205.17.207, vendor=0, lineCode=2), classId=157352751
vcpnode.log.2018-12-12.0-[10.0.35.120]:2018-12-12/09:27:26.486|10.0.35.120|api-vcp.vipkid.com.cn-9215-4840864797-0-1544578046.454|0|^_^|[http-nio-8080-exec-711] INFO  com.vipkid.vcpnode.controller.IdcNodeController 51 - IdcNodeController.compareIP false -> clientIP=117.59.84.38, req=IdcNodeReq(userId=13830152, role=STUDENT, supplierCode=null, ip=60.232.129.102, vendor=0, lineCode=3), classId=155208252
vcpnode.log.2018-12-12.0-[10.0.35.120]:2018-12-12/09:58:19.324|10.0.35.120|api-vcp.vipkid.com.cn-21907-4712832808-0-1544579899.281|0|^_^|[http-nio-8080-exec-725] INFO  com.vipkid.vcpnode.controller.IdcNodeController 51 - IdcNodeController.compareIP false -> clientIP=124.197.74.18, req=IdcNodeReq(userId=12267320, role=STUDENT, supplierCode=null, ip=103.1.138.150, vendor=0, lineCode=3), classId=155633715
vcpnode.log.2018-12-12.0-[10.0.35.120]:2018-12-12/10:24:44.235|10.0.35.120|api-vcp.vipkid.com.cn-49419-4776960233-0-1544581484.234|0|^_^|[http-nio-8080-exec-695] INFO  com.vipkid.vcpnode.controller.IdcNodeController 51 - IdcNodeController.compareIP false -> clientIP=100.71.9.45, req=IdcNodeReq(userId=9750705, role=STUDENT, supplierCode=null, ip=58.100.188.73, vendor=0, lineCode=3), classId=159090246
vcpnode.log.2018-12-12.0-[10.0.35.120]:2018-12-12/10:42:30.657|10.0.35.120|api-vcp.vipkid.com.cn-17874-4839330325-0-1544582550.632|0|^_^|[http-nio-8080-exec-697] INFO  com.vipkid.vcpnode.controller.IdcNodeController 51 - IdcNodeController.compareIP false -> clientIP=135.0.111.5, req=IdcNodeReq(userId=21317397, role=STUDENT, supplierCode=null, ip=, vendor=0, lineCode=4), classId=156118345
vcpnode.log.2018-12-12.0-[10.0.35.120]:2018-12-12/10:50:30.845|10.0.35.120|api-vcp.vipkid.com.cn-63650-4776304192-0-1544583030.807|0|^_^|[http-nio-8080-exec-727] INFO  com.vipkid.vcpnode.controller.IdcNodeController 51 - IdcNodeController.compareIP false -> clientIP=10.23.142.79, req=IdcNodeReq(userId=1928736, role=STUDENT, supplierCode=null, ip=60.232.129.207, vendor=0, lineCode=4), classId=157590017
vcpnode.log.2018-12-12.0-[10.0.35.120]:2018-12-12/12:00:37.923|10.0.35.120|api-vcp.vipkid.com.cn-17874-4840662563-0-1544587237.901|0|^_^|[http-nio-8080-exec-710] INFO  com.vipkid.vcpnode.controller.IdcNodeController 51 - IdcNodeController.compareIP false -> clientIP=120.224.7.73, req=IdcNodeReq(userId=12755690, role=STUDENT, supplierCode=null, ip=120.224.7.66, vendor=0, lineCode=4), classId=155230923
vcpnode.log.2018-12-12.0-[10.0.35.120]:2018-12-12/12:04:54.473|10.0.35.120|api-vcp.vipkid.com.cn-87286-3136455702-0-1544587494.449|0|^_^|[http-nio-8080-exec-711] INFO  com.vipkid.vcpnode.controller.IdcNodeController 51 - IdcNodeController.compareIP false -> clientIP=120.224.7.73, req=IdcNodeReq(userId=12755690, role=STUDENT, supplierCode=null, ip=120.224.7.66, vendor=0, lineCode=4), classId=155230923
vcpnode.log.2018-12-12.0-[10.0.35.120]:2018-12-12/12:20:25.928|10.0.35.120|api-vcp.vipkid.com.cn-49419-4778939663-0-1544588425.921|0|^_^|[http-nio-8080-exec-725] INFO  com.vipkid.vcpnode.controller.IdcNodeController 51 - IdcNodeController.compareIP false -> clientIP=120.224.7.73, req=IdcNodeReq(userId=12755690, role=STUDENT, supplierCode=null, ip=120.224.7.66, vendor=0, lineCode=4), classId=155230923

要得到这样的结果:

clientIP=101.241.195.137,ip=219.143.155.44
clientIP=112.0.107.254,ip=218.205.17.207
clientIP=117.59.84.38,ip=60.232.129.102
clientIP=124.197.74.18,ip=103.1.138.150
clientIP=100.71.9.45,ip=58.100.188.73
....

使用 comb 很容易把你感兴趣的字段像梳子一样给梳下来:

perl6 -ne ".comb(/clientIP'='\d+\.\d+\.\d+\.\d+ | ip'='\d+\.\d+\.\d+\.\d+/).join(',').say" ip.txt

推荐阅读更多精彩内容