使用AWK组织文件

时间:2018-06-05 01:26:53

标签: awk dataset transpose organization

好吧,我有以下文件:

Progeny Sire    Dam    Sex  PENAS       P35         P41
13254   11908   11421   M   47.275811   1322.828674 1719.183748
13323   11335   11386   M   43.29896    1225.57111  1634.436447
13562   11864   11895   M   47.884191   1228.568357 1615.427502
13338   11335   11970   M   45.780973   1196.32757  1561.900145

我需要将PENAS,P35和P41转置为柱子。这些列将是新列:age。在视觉上,我需要这样的文件:

Progeny Sire    Dam     Sex AGE     Peso
13254   11908   11421   M   PENAS   47.275811
13254   11908   11421   M   P35     1322.828674
13254   11908   11421   M   P41     1719.183748
13323   11335   11386   M   PENAS   43.29896
13323   11335   11386   M   P35     1225.57111
13323   11335   11386   M   P41     1634.436447
13562   11864   11895   M   PENAS   47.884191
13562   11864   11895   M   P35     1228.568357
13562   11864   11895   M   P41     1615.427502
13338   11335   11970   M   PENAS   45.780973
13338   11335   11970   M   P35     1196.32757
13338   11335   11970   M   P41     1561.900145

我尝试了这个命令,但它不起作用:

awk 'NR==1{h=$1 OFS $2 OFS $3 OFS $4 OFS $5 OFS $6 OFS $7; next}
            {a[$2]=(($1 in a)?(a[$1] OFS $NF):(OFS $5 OFS $6 OFS $7 OFS "AGE"));
             if(!($7 in b)) {h=h OFS $7; b[$7]}}
        END{print h; for(k in a) print k,a[k]}' a.txt | column -t > b

我在那时陷入困境,有什么建议吗?谢谢。 请注意,我原来的daset有1400行。

4 个答案:

答案 0 :(得分:4)

这是另一个awk,不依赖于列数......

$ awk 'NR==1{n=split($0,h); 
             for(i=1;i<=NF-3;i++) printf "%s", $i OFS; 
             printf "%s\n", "AGE" OFS "Peso"; next} 
            {split($0,p); 
             NF--; 
             for(i=1;i<=3;i++) 
               {$(NF-1)=h[NF-2+i]; 
                $NF=p[NF-2+i]; 
                print}}' file | column -t


Progeny  Sire   Dam    Sex  AGE    Peso
13254    11908  11421  M    PENAS  47.275811
13254    11908  11421  M    P35    1322.828674
13254    11908  11421  M    P41    1719.183748
13323    11335  11386  M    PENAS  43.29896
13323    11335  11386  M    P35    1225.57111
13323    11335  11386  M    P41    1634.436447
13562    11864  11895  M    PENAS  47.884191
13562    11864  11895  M    P35    1228.568357
13562    11864  11895  M    P41    1615.427502
13338    11335  11970  M    PENAS  45.780973
13338    11335  11970  M    P35    1196.32757
13338    11335  11970  M    P41    1561.900145

答案 1 :(得分:4)

使用GNU awk for gensub():

$ cat tst.awk
BEGIN { numPfx=4 }
{ pfx = gensub("((\\S+\\s+){"numPfx"}).*","\\1",1) }
NR==1 {
    split($0,ages)
    print pfx, "AGE", "Peso"
    next
}
{
    for (i=numPfx+1; i<=NF; i++) {
        print pfx, ages[i], $i
    }
}

$ awk -f tst.awk file | column -t
Progeny  Sire   Dam    Sex  AGE    Peso
13254    11908  11421  M    PENAS  47.275811
13254    11908  11421  M    P35    1322.828674
13254    11908  11421  M    P41    1719.183748
13323    11335  11386  M    PENAS  43.29896
13323    11335  11386  M    P35    1225.57111
13323    11335  11386  M    P41    1634.436447
13562    11864  11895  M    PENAS  47.884191
13562    11864  11895  M    P35    1228.568357
13562    11864  11895  M    P41    1615.427502
13338    11335  11970  M    PENAS  45.780973
13338    11335  11970  M    P35    1196.32757
13338    11335  11970  M    P41    1561.900145

对于其他问题,您只需将变量加上sub()替换为gensub(),并将\\S替换为[^[:space:]],将\\s替换为[[:space:]]。< / p>

答案 2 :(得分:3)

我会这样做:

transpose.awk

NR == 1 { 
  NF     -= 2         # Remove last two header columns
  $NF     = "AGE"     # Add AGE column header
  $(NF+1) = "Peso"    # Add Peso column header
  print               # Print header
  next                # Skip to next line
}

{
  for (i=5; i<=7; i++) {
    if(i==5) s = "PENAS"
    if(i==6) s = "P35"
    if(i==7) s = "P41"

    print $1, $2, $3, $4, s, $i
  }
}

运行它,例如:

awk -v OFS='\t' -f transpose.awk infile

输出:

Progeny Sire    Dam     Sex AGE     Peso
13254   11908   11421   M   PENAS   47.275811
13254   11908   11421   M   P35     1322.828674
13254   11908   11421   M   P41     1719.183748
13323   11335   11386   M   PENAS   43.29896
13323   11335   11386   M   P35     1225.57111
13323   11335   11386   M   P41     1634.436447
13562   11864   11895   M   PENAS   47.884191
13562   11864   11895   M   P35     1228.568357
13562   11864   11895   M   P41     1615.427502
13338   11335   11970   M   PENAS   45.780973
13338   11335   11970   M   P35     1196.32757
13338   11335   11970   M   P41     1561.900145

警告

请注意 EdMorton 的评论中有关弄乱NF的警告。

答案 3 :(得分:1)

关注undefined undefined //defined 也可能有所帮助。

awk