Text-Processing

如何將除幾列之外的整行分配給數組索引

  • April 4, 2018

我操作的三個類似的文本文件都包含 8 個欄位,看起來像:

File1:
145794  MEG -0.249553181556742  10.7024481844394    -2.66596894783271   0.00786178413495372 0.0601441357290167  -2.83690413867182    
144476  ACK 0.290866506987197   8.80253427197157    2.61876968039967    0.00902429557487308 0.0658077662524982  -2.9436012186193
213406  ANX -0.396398224155449  2.29892473132668    -2.67676795263515   0.00761548692081231 0.0589487734598402  -3.08353414541367

File2:
145794  MEG -0.287372579587733  9.41247425082302    -1.97898175477022   0.0490956252910287  0.309158729905081   -4.08090215553766
144476  ACK 0.298963990141891   6.98503289865222    1.45624070508412    0.146786315195169   0.492695221406182   -4.92059721623638
144161  ZCH -0.0427538540851567 9.7901544903037 -1.06263504958352   0.288330305382714   0.545733380330329   -5.69279245581735

File3:
145794  MEG -0.0565314859334139 11.3691862642502    -0.581110824476088  0.56158942073398    0.792421821608773   -5.90344886517337
144476  ACK 0.25574693016845    9.21536363194302    1.89547789943679    0.0589622388458051  0.247543472790285   -4.34643638570199
144161  ZCH -0.0618203872359407 9.77324184266448    -1.14042457462774   0.254992692808188   0.539040584775996   -5.42726538436031

我想將這三個文件與匹配的第一個和第二個欄位中的值合併,所以我的程式碼將如下所示:

awk '
 FNR==1{
   f++
 }
 f==1{
   g[$1]=$0; 
   next
 }
 f==2{
   if($1 in g) g[$1]=g[$1]"\t"$3"\t"$4"\t"$5"\t"$6"\t"$7"\t"$8; 
   else g[$1]=$0
 }
 f==3{
   if($1 in g) g[$1]=g[$1]"\t"$3"\t"$4"\t"$5"\t"$6"\t"$7"\t"$8; 
   else g[$1]=$0
 }
 END{
   for (i in g) print g[i]
 }
' file1.txt file2.txt file3.txt > merged.txt

預期輸出:

145794  MEG -0.249553181556742  10.7024481844394    -2.66596894783271   0.00786178413495372 0.0601441357290167  -2.83690413867182   -0.287372579587733  9.41247425082302    -1.97898175477022   0.0490956252910287  0.309158729905081   -4.08090215553766   -0.0565314859334139 11.3691862642502    -0.581110824476088  0.56158942073398    0.792421821608773   -5.90344886517337
144476  ACK 0.290866506987197   8.80253427197157    2.61876968039967    0.00902429557487308 0.0658077662524982  -2.9436012186193    0.298963990141891   6.98503289865222    1.45624070508412    0.146786315195169   0.492695221406182   -4.92059721623638   0.25574693016845    9.21536363194302    1.89547789943679    0.0589622388458051  0.247543472790285   -4.34643638570199
213406  ANX -0.396398224155449  2.29892473132668    -2.67676795263515   0.00761548692081231 0.0589487734598402  -3.08353414541367
144161  ZCH -0.0427538540851567 9.7901544903037 -1.06263504958352   0.288330305382714   0.545733380330329   -5.69279245581735   -0.0618203872359407 9.77324184266448    -1.14042457462774   0.254992692808188   0.539040584775996   -5.42726538436031

但是

g[$1]=g[$1]"\t"$3"\t"$4"\t"$5"\t"$6"\t"$7"\t"$8

太長了,有沒有更乾淨的方法來替換它?

cat+**awk**解決方案:

cat file[123].txt \
| awk '{ k = $1 OFS $2 }
      !(k in a){ keys[++c] = k }
      { a[k] = (a[k]? a[k] OFS : "") $3 OFS $4 OFS $5 OFS $6 OFS $7 OFS $8; }
      END{ 
          for (i=1; i<=c; i++) print keys[i], a[keys[i]] 
      }' OFS='\t'
  • k = $1 OFS $2``$1- 由第 1 欄位和第 2 欄位組合而成的關鍵鍵$2
  • !(k in a){ keys[++c] = k }-keys是一個數組,包含k按初始順序排列的組鍵
  • a[k] = (a[k]? a[k] OFS : "") $3 OFS $4 ...- 擷取/附加與某個組相關的所有需要​​的欄位值k

輸出:

145794  MEG -0.249553181556742  10.7024481844394    -2.66596894783271   0.00786178413495372 0.0601441357290167  -2.83690413867182   -0.287372579587733  9.41247425082302    -1.97898175477022   0.0490956252910287  0.309158729905081   -4.08090215553766   -0.0565314859334139 11.3691862642502    -0.581110824476088  0.56158942073398    0.792421821608773   -5.90344886517337
144476  ACK 0.290866506987197   8.80253427197157    2.61876968039967    0.00902429557487308 0.0658077662524982  -2.9436012186193    0.298963990141891   6.98503289865222    1.45624070508412    0.146786315195169   0.492695221406182   -4.92059721623638   0.25574693016845    9.21536363194302    1.89547789943679    0.0589622388458051  0.247543472790285   -4.34643638570199
213406  ANX -0.396398224155449  2.29892473132668    -2.67676795263515   0.00761548692081231 0.0589487734598402  -3.08353414541367
144161  ZCH -0.0427538540851567 9.7901544903037 -1.06263504958352   0.288330305382714   0.545733380330329   -5.69279245581735   -0.0618203872359407 9.77324184266448    -1.14042457462774   0.254992692808188   0.539040584775996   -5.42726538436031

引用自:https://unix.stackexchange.com/questions/435432