2013-04-11 163 views
1

希望有人能建議以下內容。將awk輸出分配給shell變量

目前我有以下的代碼,我是想分配$ 24和$ 5的{}括號內的shell變量 - 這是可能的:

while read line; do pntadm -P $line | awk '{if (($2 == 00 && $1 != 00) || ($2 == 04)) print $3,$5}'; done < /tmp/subnet_list 

爲了讓事情變得更加清晰:

root[my-box]# cat /tmp/final_list 
111.222.333.0 
root[my-box]# pntadm -P 111.222.333.0 | head 

Client ID  Flags Client IP  Server IP  Lease Expiration    Macro   Comment 

00A0BCDE1FGHI1 00  111.222.333.001 111.222.333.253  04/06/2013      macro1 
00    04  111.222.333.002 111.222.333.253  Zero       macro1 
00    00  111.222.333.003 111.222.333.253  Zero       macro1 
00A0BCDE1FGHI2 00  111.222.333.004 111.222.333.253  05/06/2013      macro1 
root[my-box]# 

我有一份禮物的代碼應該從上面選出三行(IP的111.222.333.001 ... 002和004)。

如果我只是需要例如$ 5我能做到以下幾點:

while read line; do date=$(pntadm -P $line | awk '{if (($2 == 00 && $1 != 00) || ($2 == 04)) print $5}'); done < /tmp/subnet_list 

但我同時需要$ 90和$ 5在一起......

不使用數組的,誰能告訴我如何分配$ 24和$ 5至shell變量,請在{}括號內?

RGDS

東歐

+0

這可能會幫助:http://stackoverflow.com/a/9001927/1983854 – fedorqui 2013-04-11 09:34:23

回答

2

不能指定shell變量的「{}括號內」 - 你對環境的AWK過程中會消失awk的進程退出的時候什麼。你需要從awk打印你需要的數據並從shell中讀取它。試試這個:

while read line; do 
    pntadm -P $line | 
    awk '($2 == "00" && $1 != "00") || ($2 == "04") {print $3,$5}' | 
    while read client_ip lease_exp; do 
     : do something with $client_ip and $lease_exp 
    done 
done < /tmp/subnet_list 

或者不AWK

while read line; do 
    pntadm -P $line | 
    while read clientID flags clientIP serverIP leaseExpiration macro comment; do 
     if [[ ($flags == "00" && $clientID != "00") || ($flags == "04") ]]; then 
      : do something with $clientIP and $leaseExpiration 
     fi 
    done 
done < /tmp/subnet_list 
+0

尼斯之一!正是我在尋找(在一段時間內):)感謝Glenn – user2269537 2013-04-11 10:37:23