Como atribuir a linha inteira, exceto várias colunas, a um índice de array

1

Eu operei três arquivos de texto semelhantes, todos contendo 8 campos e parecidos com:

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

Eu quero mesclar esses três arquivos com os valores no primeiro e segundo campos correspondentes, para que meu código se pareça com:

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

A saída esperada:

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

Mas o

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

é muito longo, existe alguma maneira mais limpa de substituir isso?

    
por MagicPants 04.04.2018 / 08:19

1 resposta

1
Solução

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 - chave crucial feita pela combinação do primeiro campo $1 e do segundo campo $2
  • !(k in a){ keys[++c] = k } - keys é uma matriz contendo as chaves de grupo k em sua ordem inicial
  • a[k] = (a[k]? a[k] OFS : "") $3 OFS $4 ... - captura / anexa todos os valores de campo necessários relacionados a um determinado grupo k

A saída:

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
    
por 04.04.2018 / 11:27