gpt4 book ai didi

shell - 如果它们不匹配,如何从awk中选择两列并打印

转载 作者:行者123 更新时间:2023-12-01 11:14:12 25 4
gpt4 key购买 nike

我需要从 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","responseCode":"18","description":"OMO account migration – 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 值来获取 token 号。然后我使用这些标记来获取多个参数。最终输出是这样的:

Date            Time          Old MSISDN        New MSISDN     Old Profile New Profile  CNIC      Acc Status Acc Status Migration Channel
(Before) (After)
2019-03-11 | 00:00:14 | 923135260528 | 923029403541 | OMO BVS MA | 0 | 1620221953175 | ACTIVE | | subapp

2019-03-11 | 00:00:14 | 923135260528 | 923003026654 | OMO BVS MA | 0 | 1620221953175 | ACTIVE | | subapp

2019-03-11 | 00:00:14 | 923135260528 | 923003026654 | OMO BVS MA | 0 | 1620221953175 | ACTIVE | | subapp

2019-03-11 | 00:00:14 | 923135260528 | 923038048244 | OMO BVS MA | 0 | 1620221953175 | ACTIVE | | subapp

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

最佳答案

第一版问题的答案

尝试:

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

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

工作原理

  • -F'[*][*]'

    这会将字段分隔符设置为两颗星。这样新的MSISDN就是字段2,旧的就是字段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

关于shell - 如果它们不匹配,如何从awk中选择两列并打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55134913/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com