【问题标题】:Column entry to header标题的列条目
【发布时间】:2020-09-26 04:46:15
【问题描述】:

我有一个格式如下的文件;其中第 3 列是 id(条件),第 2 列是样本,第 1 列是数字。数据具有 4 个唯一 ID/条件(A、B、C、U)。我想将数据转换为ID(A,B,C,U)成为标题并在其下具有相应数字的格式。

输入文件

No      Sample  Id
131     E0233   A
202     E0233   B
326     E0233   C
419     E0233   U
28      E0412   A
42      E0412   B
79      E0412   C
95      E0412   U
8       E0442   A
10      E0442   B
8       E0442   C
19      E0442   U
72      E1008   A
67      E1008   B
176     E1008   C
169     E1008   U
8       E2125   A
11      E2125   B
13      E2125   C
24      E2125   U
9       E2165   A
27      E2165   B
25      E2165   C
35      E2165   U
10      E2501   A
19      E2501   B
53      E2501   C
40      E2501   U
119     E2596   A
166     E2596   B
393     E2596   C
342     E2596   U
1       E2829   B
93      E3100   A
167     E3100   B
297     E3100   C
301     E3100   U
3       E4982   A
1       E4982   B

输出文件

Sample  A   B   C   U
E0233   131 202 326 419
E0412   28  42  79  95
E0442   8   10  8   19
E1008   72  67  176 169
E2125   8   11  13  24
E2165   9   27  25  35
E2501   10  19  53  40
E2596   119 166 393 342
E2829   0   1   0   0
E3100   93  167 297 301
E4982   3   1   0   0

因为我刚刚收到一条评论说 SO 不是要问的;我只是想补充一点,虽然我在这里问了这个问题,但我也在尝试通过 SO 中建议的以前的解决方案,以便我可以解决它。我正在尝试使用的最新版本是 (Transpose in Unix)。我自己也在尝试;但绝对我的知识是有限的。谢谢

【问题讨论】:

  • 请以代码的形式添加您的努力,这是非常鼓励的,谢谢
  • Stackoverflow 不是要问的,见:stackoverflow.com/help/how-to-ask
  • 你尝试了一些东西,但它“不起作用”。我认为 SO 是一个供想要学习的用户使用的平台。如果您尝试过某事,则将您尝试过的内容添加到问题中,以及为什么期望没有达到的信息。 (=比“它不工作”!或“我的知识有限”更多的信息!)。通过这种方式,有人可以提供提示/技巧,您将扩展您的知识。
  • @Luuk 我所说的我所拥有的任何知识都来自 SO。我给了我想要修改的链接;但这没有用。所以,寻求社区帮助。如果你让我做关于冠状病毒的讲座,我可以解释所有基础知识;但是在计算方面,我仍然是一个学习者。
  • 任何时候你发布需要滚动条来阅读的示例输入或输出,你会大大减少愿意帮助你的人的数量,因为这意味着你没有付出必要的努力来制作你的例子minimal 让我们尽可能轻松地做到这一点。

标签: shell awk


【解决方案1】:

您能否尝试在 GNU awk 中使用所示示例进行跟踪、编写和测试。

awk '
FNR>1{
  sample[$2]
  value[$2,$3]=$1
  if(!a[$3]++){
    b[++count]=$3
  }
}
END{
  printf("%s ","sample")
  for(i=1;i<=count;i++){
    printf("%s%s",b[i],(i==count?ORS:OFS))
  }
  for(k in sample){
    printf("%s ",k)
    for(i=1;i<=count;i++){
       printf("%d %s",(value[k,b[i]]!=""?value[k,b[i]]:""),(i==count?ORS:OFS))
    }
  }
}' Input_file

说明:为上述添加详细说明。

awk '                                        ##Starting awk program from here.
FNR>1{                                       ##Checking condition if line number is greater than 1 then do following.
  sample[$2]                                 ##Creating sample array with index of 2nd column.
  value[$2,$3]=$1                            ##Creating value with index of 2nd and 3rd column and value is 1st field.
  if(!a[$3]++){                              ##Checking condition if 3rd field is found in a first time then do following.
    b[++count]=$3                            ##Creating b with index of count increasing with 1 each time cursor comes here with value of 3rd field in it.
  }
}
END{                                         ##Starting END block of this awk program. from here.
  printf("%s ","sample")                     ##Printing sample text as a header before printing anything here.
  for(i=1;i<=count;i++){                     ##Running a for loop till value of count here.
    printf("%s%s",b[i],(i==count?ORS:OFS))   ##Printing all header values with spaces and printing new line at last.
  }
  for(k in sample){                          ##Traversing through sample array here.
    printf("%s ",k)                          ##printing value of k value.
    for(i=1;i<=count;i++){                   ##Running a for loop till value of count here.
       printf("%d %s",(value[k,b[i]]!=""?value[k,b[i]]:""),(i==count?ORS:OFS))  ##Printing value of array value with index of k and value of b[i] and printing new line at end of loop and spaces for each element.
    }
  }
}' Input_file                                ##Mentioning Input_file name here.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多