[英]compare two columns in awk and print values from lookup files into output file
[英]How to select two columns from awk and print if they do not match
我需要从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 | 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,旧的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
考虑到您的实际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.
我会采用以下方法:我看到每个MSISDN号码包含12个数字([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).
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.