2014-02-26 30 views
0

我有多個線的命令,並找到細節AWK搜索和分析數據

runmqsc -cert -details -pw test1 -db 123.kdb -label "Mahendra1" 

輸出:

label : mahendra1 
issuer : cn=mahendra2, OS=abcd 
subject : 
---------- 
=mahendra3, os=hkjkj 
Not Before : 10 oct 2013 
not after : 20 oct 2014 

現在我要提取從上述命令的發行者的值(mahendra2),並有下一個迭代命令。

runmqsc -cert -details -pw test1 -db 123.kdb -label "Mahendra2" 

輸出:

label : mahendra2 
issuer : cn=mahendra3, OS=abcd 
subject :CN=mahendra4, os=hkjkj 
Not Before : 10 oct 2014 
not after : 20 oct 2015 

在時刻發放者和對象值中的一個點將同。即可能5-6次迭代。

label : mahendra5 
issuer : cn=mahendra6, OS=abcd 
subject :CN=mahendra6, os=hkjkj 
Not Before : 10 oct 2014 
not after : 20 oct 2015 

,有沒有辦法在awk來執行呢?

+0

所以你想循環'runmqsc'命令,直到'issuer'和'subject'行匹配(或匹配'cn =')?或者你只想總共執行5-6次迭代?你想保留每次迭代的信息嗎?還是隻需要最後一個副本?你有什麼嘗試? – n0741337

+0

是的,我想結束runmqsc命令的循環結束,直到發行者和主題行匹配 – Mahendra

回答

1

要回答這個問題:

is there any way to execute in awk ? 

這是極有可能,你可以使用awk執行這個更多。我實際上有一箇舊的gawk鏈接爲awk。我很好奇它是否完全可以用「awk」來完成,所以我們現在就是這樣。

請注意,這是完全濫用awk,因爲awk設計用於處理輸入文件。但是,如果將所有邏輯放入BEGIN塊中,則您所要求的技術上是可行的。我沒有你的命令,所以我用cat嘲笑了這個行爲,其中包含你的label和發行者字段。我把下列一個名爲abuse文件:

#!/usr/bin/awk -f 

BEGIN { 
    label="mahendra1" 
    # j is a safety so you do not infinitely loop 
    while(abuseAwk(withCmdForLabel()) && ++j < 10) { 
     # do nothing here 
    } 
} 

# There is no input file, so the body will never execute anything. 
# This is *why* this script is an abuse of awk 
# Putting commands here would cause awk to "hang", while waiting for input 
# An END block would also "hang" for the same reason. 

function abuseAwk(cmd) { 
    printf("cmd = %s\n", cmd) 
    while(cmd | getline) { 
     FS="=" # set this here after getline has assigned $0 
     if($0 ~ /^issuer/) { i = $2; sub(/,.*$/, "", i); label = i } 
     if($0 ~ /^subject/) { s = $2; sub(/,.*$/, "", s) } 
    } 
    close(cmd) 

    return(i != s) 
} 

# you could rewrite the sprintf for your command 
function withCmdForLabel() { return(sprintf("cat %s", label)) } 

chmod +x abuse使其可執行。第一個label是硬編碼的,以保持純粹的awk領域。只有輸出是命令的形式,因爲它在abuseAwk()函數中運行。

一個更好的選擇是使用bash或其他腳本語言而進行過命令循環(這可以養活一個非常簡單的awk腳本的話),你應該永遠把這個代碼投入生產。