首页 > 解决方案 > 如果它们不匹配,如何从 awk 中选择两列并打印

问题描述

我需要从 OMO 帐户迁移日志中选择两个 MSISDN 值并打印不匹配的值。

[2019-03-11 04:15:08 INFO-SUBAPP ESBRestClient:117] ## IP-103.228.158.85##TOKEN-201903110416276787774(**923419606907**)RESPONSE-BODY: {"callStatus":"false","responseCode":"18","description":"OMO account migration – **923481057772**"}

[2019-03-11 04:24:02 INFO-SUBAPP ESBRestClient:117] ## IP-119.153.134.128##TOKEN-1552260212780839(923214748517)RESPONSE-BODY: {"callStatus":"false","re​​sponseCode": "18","description":"OMO账户迁移-953214748517"}

923481057772是旧的MSISDN。

923419606907是新的 MSISDN,我需要将它保存在一个新文件中。我正在使用以下命令仅选择新的 MSISDN:

cat migration.txt | egrep "OMO account migration" | egrep "responseCode\":\"1700" | awk -F"(" '{gsub(/\).*/,"",$2);print $2}' >>newmsisdn.txt

我正在使用保存的 msisdn 值来获取令牌号。然后我使用这些令牌来获取多个参数。最终输出是这样的:

日期时间旧MSISDN新MSISDN旧配置文件新配置文件CNIC Acc状态Acc状态迁移通道(之前)(之后)2019-03-11 | 00:00:14 | 923135260528 | 923029403541 | OMO BVS MA | 0 | 1620221953175 | 活跃 | | 子应用

2019-03-11 | 00:00:14 | 923135260528 | 923003026654 | OMO BVS MA | 0 | 1620221953175 | 活跃 | | 子应用

2019-03-11 | 00:00:14 | 923135260528 | 923003026654 | OMO BVS MA | 0 | 1620221953175 | 活跃 | | 子应用

2019-03-11 | 00:00:14 | 923135260528 | 923038048244 | OMO BVS MA | 0 | 1620221953175 | 活跃 | | 子应用

在第二个日志实例中,这两个值是相同的。我需要过滤掉那些,即我只需要使用不匹配的值。如何比较两个不匹配的值并打印新的 MSISDN?

标签: shellunixawkgrep

解决方案


回答问题的第一个版本

尝试:

awk -F'[*][*]' '/OMO account migration/ && /responseCode":"18"/ && $2 != $4 { print $2}' migration.txt

这避免了产生多个进程并将它们与管道连接的需要。这使得这种方法相对有效。

这个怎么运作

  • -F'[*][*]'

    这将字段分隔符设置为两颗星。这样,新的 MSISDN 是字段 2,旧的 MSISDN 是字段 4。

  • /OMO account migration/ && /responseCode":"18"/ && $2 != $4 { print $4}

    这将选择 (1) 包含正则表达式OMO account migration/ (2) 包含正则表达式responseCode":"18" (3) 具有与第四个不同的第二个字段的行。对于任何这样的行,将打印第二个字段。

例子

让我们考虑这个三行测试文件:

$ cat migration.txt 
[2019-03-11 04:15:08 INFO-SUBAPP ESBRestClient:117] ## IP-103.228.158.85##TOKEN-201903110416276787774(**923419606907**)RESPONSE-BODY: {"callStatus":"false","responseCode":"18","description":"OMO account migration – **923481057772**"}
[2019-03-11 04:15:08 INFO-SUBAPP ESBRestClient:117] ## IP-103.228.158.85##TOKEN-201903110416276787774(**923419606888**)RESPONSE-BODY: {"callStatus":"false","responseCode":"19","description":"OMO account migration – **923481057999**"}
[2019-03-11 04:15:08 INFO-SUBAPP ESBRestClient:117] ## IP-103.228.158.85##TOKEN-201903110416276787774(**923419606123**)RESPONSE-BODY: {"callStatus":"false","responseCode":"18","description":"OMO account migration – **923419606123**"}

让我们运行我们的命令:

$ awk -F'[*][*]' '/OMO account migration/ && /responseCode":"18"/ && $2 != $4 {print $2}' migration.txt >>newmsisdn.txt

输出文件现在包含我们想要的一个新 MSISDN:

$ cat newmsisdn.txt 
923419606907

推荐阅读