2016-11-11 239 views
-1

我已經txt文件與下面的值如何使用awk獲取鍵值對?

f:0 
c:Test C 
s:test S 
ctype:0 
a:test A 
t:10 
r:test r 

f:0 
c:Test C1 
s:test S1 
ctype:1 
a:test A1 
t:10 
r:test r 

f:20 
c:Test C 
s:test S 
ctype:2 
a:test A1 
t:0 
r:test r 

我曾嘗試下面的代碼,但它是不是給所有單個新行的值作爲鍵值對環雙新行適當的輸出:

awk ' 
    BEGIN { 
    FS="\n" 
    RS="" 
} 
{ 
    print $1 "\n " $2 "\n" $3 
} 

可以使用awk以外的任何東西。

編輯:

輸出會是這樣:

if(ctype==0){ 
execute c; 
}else if(ctype==1){ execute a} 

我想循環中的數據並執行操作。

+0

'awk -F:'NF {print $ 1,$ 2}'file' – anubhava

+5

預期輸出是什麼? – user000001

+1

告訴我們一個命令不會做你想做的事,告訴我們它沒有做你想做的事情,不會告訴我們你想做什麼! –

回答

0

我不能完全肯定我明白你的問題,但是這是接近你心目中是什麼:

{ 
    if (/^a:/) a = substr($0, 3) 
    if (/^c:/) c = substr($0, 3) 
    if (/^ctype:/) ctype = substr($0, 7) 
    if (/^$/) { 
     if (ctype == "0") { 
      print "a: [", a, "]" 
     } else { 
      print "c: [", c, "]" 
     } 
    } 
} 
END { 
    if (ctype == "0") { 
     print "a: [", a, "]" 
    } else { 
     print "c: [", c, "]" 
    } 
} 

這讀取測試的每個塊,尋找A,C和CTYPE的值,然後根據ctype的值打印a或c的值。這就是你的問題的編輯部分似乎表明你想要完成;如果沒有,希望它至少能指引你朝着正確的方向前進。

相關問題