[英]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.