2017-05-12 140 views
4

我正在處理解析文件的Awk/Gawk腳本,爲每行填充多維數組。第一列是以句點分隔的字符串,每個值都是對下一級別數組鍵的引用。第二列是值動態填充多維awk數組

這裏是正在分析什麼內容示例如下:

$ echo -e "personal.name.first\t= John\npersonal.name.last\t= Doe\npersonal.other.dob\t= 05/07/87\npersonal.contact.phone\t= 602123456\npersonal.contact.email\t= [email protected]\nemployment.jobs.1\t= Company One\nemployment.jobs.2\t= Company Two\nemployment.jobs.3\t= Company Three" 
personal.name.first  = John 
personal.name.last  = Doe 
personal.other.dob  = 05/07/87 
personal.contact.phone = 602123456 
personal.contact.email = [email protected] 
employment.jobs.1  = Company One 
employment.jobs.2  = Company Two 
employment.jobs.3  = Company Three 

其中被解析後,林期待它具有相同的結構爲:

data["personal"]["name"]["first"]  = "John" 
data["personal"]["name"]["last"]  = "Doe" 
data["personal"]["other"]["dob"]  = "05/07/87" 
data["personal"]["contact"]["phone"] = "602123456" 
data["personal"]["contact"]["email"] = "[email protected]" 
data["employment"]["jobs"]["1"]  = Company One 
data["employment"]["jobs"]["2"]  = Company Two 
data["employment"]["jobs"]["3"]  = Company Three 

我卡在的部分是如何動態地在構造多維數組時填充鍵。

我發現this SO thread,涵蓋了類似的問題,這是通過使用SUBSEP變量,起初似乎是因爲我需要它的工作解決了,但一些測試後,它看起來像arr["foo", "bar"] = "baz"沒有得到像對待真正的數組,如arr["foo"]["bar"] = "baz"會。什麼,我的意思了一個例子是在陣列中任何級別的計數值不能:arr["foo", "bar"] = "baz"; print length(arr["foo"])只會打印0(零)

我發現this SO thread這有助於一點點,可能是指着我在正確的方向。

中提到在線程的代碼段:

BEGIN { 
    x=SUBSEP 

    a="Red" x "Green" x "Blue" 
    b="Yellow" x "Cyan" x "Purple" 

    Colors[1][0] = "" 
    Colors[2][0] = "" 

    split(a, Colors[1], x) 
    split(b, Colors[2], x) 

    print Colors[2][3] 
} 

是非常接近的,但我現在遇到的問題是,該密鑰(例如:RedGreen等),需要指定動態地,並且可能有一個或多個鍵。

基本上,我怎麼能走a_keysb_keys字符串,通過.割裂開來,並填充ab變量多維數組?..

BEGIN { 
    x=SUBSEP 

    # How can I take these strings... 
    a_keys = "Red.Green.Blue" 
    b_keys = "Yellow.Cyan.Purple" 

    # .. And populate the array, just as this does: 
    a="Red" x "Green" x "Blue" 
    b="Yellow" x "Cyan" x "Purple" 

    Colors[1][0] = "" 
    Colors[2][0] = "" 

    split(a, Colors[1], x) 
    split(b, Colors[2], x) 

    print Colors[2][3] 
} 

任何幫助,將不勝感激,謝謝!

回答

1

所有你需要的是:

BEGIN { FS="\t= " } 
{ 
    split($1,d,/\./) 
    data[d[1]][d[2]][d[3]] = $2 
} 

看:

$ cat tst.awk 
BEGIN { FS="\t= " } 
{ 
    split($1,d,/\./) 
    data[d[1]][d[2]][d[3]] = $2 
} 
END { 
    for (x in data) 
     for (y in data[x]) 
      for (z in data[x][y]) 
       print x, y, z, "->", data[x][y][z] 
} 

$ awk -f tst.awk file 
personal other dob -> 05/07/87 
personal name first -> John 
personal name last -> Doe 
personal contact email -> [email protected] 
personal contact phone -> 602123456 
employment jobs 1 -> Company One 
employment jobs 2 -> Company Two 
employment jobs 3 -> Company Three 

以上是呆子,具體當然,因爲沒有其他的awk支持真正的多維數組。

要填充的多維數組時的索引並不總是相同的深度(例如,大於3)的,這是相當複雜:

########## 
$ cat tst.awk 
function rec_populate(a,idxs,curDepth,maxDepth,tmpIdxSet) { 
    if (tmpIdxSet) { 
     delete a[SUBSEP]    # delete scalar a[] 
     tmpIdxSet = 0 
    } 
    if (curDepth < maxDepth) { 
     # We need to ensure a[][] exists before calling populate() otherwise 
     # inside populate() a[] would be a scalar, but then we need to delete 
     # a[][] inside populate() before trying to create a[][][] because 
     # creating a[][] below creates IT as scalar. SUBSEP used arbitrarily. 

     if (!((idxs[curDepth] in a) && (SUBSEP in a[idxs[curDepth]]))) { 
      a[idxs[curDepth]][SUBSEP] # create array a[] + scalar a[][] 
      tmpIdxSet = 1 
     } 
     rec_populate(a[idxs[curDepth]],idxs,curDepth+1,maxDepth,tmpIdxSet) 
    } 
    else { 
     a[idxs[curDepth]] = $2 
    } 
} 

function populate(arr,str,sep, idxs) { 
    split(str,idxs,sep) 
    rec_populate(arr,idxs,1,length(idxs),0) 
} 

{ populate(arr,$1,",") } 

END { walk_array(arr, "arr") } 

function walk_array(arr, name,  i) 
{ 
    # Mostly copied from the following URL, just added setting of "sorted_in": 
    # https://www.gnu.org/software/gawk/manual/html_node/Walking-Arrays.html 
    PROCINFO["sorted_in"] = "@ind_str_asc" 
    for (i in arr) { 
     if (isarray(arr[i])) 
      walk_array(arr[i], (name "[" i "]")) 
     else 
      printf("%s[%s] = %s\n", name, i, arr[i]) 
    } 
} 

########## 
$ cat file 
a uno 
b,c dos 
d,e,f tres_wan 
d,e,g tres_twa 
d,e,h,i,j cinco 

########## 
$ awk -f tst.awk file 
arr[a] = uno 
arr[b][c] = dos 
arr[d][e][f] = tres_wan 
arr[d][e][g] = tres_twa 
arr[d][e][h][i][j] = cinco 
+0

我可能忘了提及的是,鑰匙可能並不總是恰好有三個部分。但是,這是一個足夠好的啓動我與之合作。謝謝! – Justin

+0

然後你需要我發佈的第二個解決方案。陳述明顯 - 如果你的真實數據有多個細分市場,那麼你的樣本輸入也應該有。 –

0

沒有真正multidim陣列,你可以做多一點簿記

awk -F'\t= ' '{split($1,k,"."); 
       k1[k[1]]; k2[k[2]]; k3[k[3]]; 
       v[k[1],k[2],k[3]]=$2} 
      END {for(i1 in k1) 
       for(i2 in k2) 
        for(i3 in k3) 
        if((i1,i2,i3) in v) 
         print i1,i2,i3," -> ",v[i1,i2,i3]}' file 


personal other dob -> 05/07/87 
personal name first -> John 
personal name last -> Doe 
personal contact email -> [email protected] 
personal contact phone -> 602123456 
employment jobs 1 -> Company One 
employment jobs 2 -> Company Two 
employment jobs 3 -> Company Three