【问题标题】:How to select two columns from awk and print if they do not match如果它们不匹配,如何从 awk 中选择两列并打印
【发布时间】:2019-03-13 05:21:48
【问题描述】:

我需要从 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账户迁移-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?

【问题讨论】:

  • 当您说filter those out 时,您的意思是print those linesprint all except those lines 还是别的什么? edit 您的问题是显示多行输入,而不仅仅是一行,有些符合您的标准,有些不符合您的标准,以及给定该输入的预期输出。
  • 我需要打印除具有相同 MSISDN 的行之外的所有行。实际上,使用新的 MSISDN,我需要获取令牌号。使用该令牌编号,我将提取多个参数。我也会发布输出。
  • Sana,如果你想得到最好和最有帮助的答案,你应该遵循@EdMorton 的建议,包括,例如,显示“多行输入,而不仅仅是一个,有些可以,有些不可以不符合你的标准。”只有你知道你的实际输入是什么样的。如果没有您提供的清晰准确的数据,我们只能猜测,猜测会浪费我们和您的时间。

标签: shell unix awk grep


【解决方案1】:

第一版问题的答案

试试:

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

【讨论】:

    【解决方案2】:

    考虑到您的实际 Input_file 与显示的示例相同,并且每行都需要新值,如果是这种情况,请尝试以下操作。

    awk '
    /OMO account migration/ && /responseCode":"18"/{
      val_old=val_new=""
      match($0,/\*\*[0-9]+\*\*/)
      val_old=substr($0,RSTART,RLENGTH)
      $0=substr($0,RSTART+RLENGTH)
      match($0,/\*\*[0-9]+\*\*/)
      val_new=substr($0,RSTART,RLENGTH)
    }
    (val_old!=val_new){
      gsub("*","",val_new)
      print val_new
    }
    '   Input_file
    

    说明:现在为上述代码添加详细说明。

    awk '                                                     ##Starting awk program here.
    /OMO account migration/ && /responseCode":"18"/{          ##Checking condition if a line contains strings OMO account migration AND responseCode":"18" in it then do following.
      val_old=val_new=""                                      ##Nullifying variables val_old and val_new here.
      match($0,/\*\*[0-9]+\*\*/)                              ##Using match OOTB function of awk to match from **digits** here. If match found then value of RSTART and RLENGTH(awk variables) will be SET.
      val_old=substr($0,RSTART,RLENGTH)                       ##Creating variable val_old which is substring of starting point as RSTART and ending point of RLENGTH here.
      $0=substr($0,RSTART+RLENGTH)                            ##Re-defining value of current line with substring whose value starts after matched regexs next index, so that we can catch new value in next further statements.
      match($0,/\*\*[0-9]+\*\*/)                              ##Using match OOTB function of awk to match from **digits** here. If match found then value of RSTART and RLENGTH(awk variables) will be SET(2nd time run).
      val_new=substr($0,RSTART,RLENGTH)                       ##Creating variable named val_new whose value is substring of current line startpoint is RSTART and ending point is RLENGTH here.
    }                                                         ##Closing BLOCK for string matching condition here.
    (val_old!=val_new){                                       ##Checking condition ig val_old variable is NOT equal to val_new then do following.
      gsub("*","",val_new)                                    ##Globaly subsituting * in val_new to get exact value as per OP need.
      print val_new                                           ##Printing val_new value here.
    }
    '  Input_file                                             ##Mentioning Input_file name here.
    

    【讨论】:

    • @SANA SIDDIQUI,您能否检查一下,如果这对您有帮助,请告诉我?
    【解决方案3】:

    我会采用以下方法:我看到每个 MSISDN 号码都包含十二位数字 ([0-9]),位于两个双星号之间。
    您可以使用以下正则表达式找到那些:

    grep -o "\*\*[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]\*\*"
    

    如果您的系统支持此功能,您可以将其简化为:

    grep -o "\*\*[0-9]{12}\*\*"
    

    一旦你有了这些,你可以使用 awk 来显示不同的地方,比如:

    '{IF ($1 != $2) PRINT $1 $2}' (not tested).
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多