2013-08-29 79 views
0

我有兩行的值,希望獲取所有值並將其設置爲變量。從兩行獲取值並將其轉換爲變量

輸出從EMC存儲:

Bus 0 Enclosure 0 Disk 0 
State:     Enabled 

Bus 0 Enclosure 0 Disk 1 
State:     Enabled 

預期結果:

Bus:0|Enclosure:0|Disk:0|State:Enabled 

或者只是需要有人給我方向如何讓最後一排...

回答

2

試試這個awk:

$ awk '/^Bus/{for(i=1;i<=NF;i+=2) printf "%s:%s|", $i,$(i+1)}/^State/{printf "%s%s\n", $1, $2}' file 
Bus:0|Enclosure:0|Disk:0|State:Enabled 
Bus:0|Enclosure:0|Disk:1|State:Enabled 

要處理多個詞語的最後一個字段,你可以這樣做:

$ awk '/^Bus/{for(i=1;i<=NF;i+=2) printf "%s:%s|", $i,$(i+1)}/^State/{printf "%s", $1; for (i=2;i<=NF;i++) printf "%s ", $i; print ""}' file 
Bus:0|Enclosure:0|Disk:0|State:Enabled 
Bus:0|Enclosure:0|Disk:1|State:hot space 
+0

也許能延長腳本舉例來說,如果沒有啓用最新的Word和恩:熱備用,那麼我們應該只看到「熱門」。在最後一欄之後如何改進以獲得一切? –

+1

@KalinBorisov更新 – user000001

2
perl -00anE 's/:// for @F; say join "|", map { $_%2 ?() : "$F[$_]:$F[$_+1]" } 0..$#F' file 

輸出

Bus:0|Enclosure:0|Disk:0|State:Enabled 
Bus:0|Enclosure:0|Disk:1|State:Enabled 
3

這可能會爲你工作(GNU SED):

sed '/^Bus/!d;N;s/[0-9]\+/:&|/g;s/\s//g' file 

要只獲得最後一行:

sed '/^Bus/{N;h};$!d;x;s/[0-9]\+/:&|/g;s/\s//g' file 
1

隨着GNU awk你可以這樣做:

$ awk 'NR>1{$6=$6$7;NF--;print RS,$0}' RS='Bus' OFS='|' file 
Bus|0|Enclosure|0|Disk|0|State:Enabled 
Bus|0|Enclosure|0|Disk|1|State:Enabled 

而且只有最後一行:

$ awk 'END{$6=$6$7;NF--;print RS,$0}' RS='Bus' OFS='|' file 
Bus|0|Enclosure|0|Disk|1|State:Enabled 
相關問題