【问题标题】:How to paste values from a tab delimited table into a second file matching a word in column 1 from the first file如何将制表符分隔表中的值粘贴到与第一个文件中第 1 列中的单词匹配的第二个文件中
【发布时间】:2019-03-04 09:50:34
【问题描述】:

我有一个 CD-hit 结果文件(对于熟悉 CD-hit 的人)。我想将另一个文件中的第二列值粘贴到相应匹配的 RUN ID 旁边的 CD-hit 文件中,该文件是一个 2 列制表符分隔的表(如示例图像中以绿色突出显示的那个)。 RUN ID 是制表符分隔表中的第一列(如下)。sample image for CD-hit file

CD-hit 结果文件(第一个文件)

>Cluster 0
0   108nt, >ERR123456.1016542.1... *
1   108nt, >ERR123456.3114223.2... at +/93.52%
2   108nt, >ERR345678.217087.1... at -/89.81%
3   108nt, >ERR345678.291581.2... at -/92.59%
4   108nt, >ERR567890.3381351.2... at +/87.96%
5   108nt, >ERR987654.126640.2... at -/86.11%
6   108nt, >ERR987654.2492930.2... at +/84.26%
7   108nt, >ERR987654.3327702.1... at +/92.59%
>Cluster 1
0   108nt, >ERR876543.626414.2... *
1   108nt, >ERR123456.3213598.2... at +/85.19%
2   108nt, >ERR567890.1158706.2... at +/97.22%
3   108nt, >ERR345678.146372.1... at -/88.89%
4   108nt, >ERR765432.201531.2... at -/92.59%
5   108nt, >ERR765432.2770540.1... at -/87.04%

制表符分隔的表格(第二个文件)

 ERR123456   1650
 ERR345678   2350
 ERR567890   1520
 ERR876543   4520
 ERR987654   3960
 ERR765432   2550

我希望输出文件包含制表符分隔表的第二列中的值,该值位于包含其各自 RUN ID 的行旁边(表中的第一列)。

>Cluster 0
0   108nt, >ERR123456.1016542.1... *             1650  #matching RUN ID
1   108nt, >ERR123456.3114223.2... at +/93.52%   1650
2   108nt, >ERR345678.217087.1... at -/89.81%    2350
3   108nt, >ERR345678.291581.2... at -/92.59%    2350
4   108nt, >ERR567890.3381351.2... at +/87.96%   1520
5   108nt, >ERR987654.126640.2... at -/86.11%    3960
6   108nt, >ERR987654.2492930.2... at +/84.26%   3960
7   108nt, >ERR987654.3327702.1... at +/92.59%   3960
>Cluster 1
0   108nt, >ERR876543.626414.2... *              4520
1   108nt, >ERR123456.3213598.2... at +/85.19%   1650
2   108nt, >ERR567890.1158706.2... at +/97.22%   1520
3   108nt, >ERR345678.146372.1... at -/88.89%    2350
4   108nt, >ERR765432.201531.2... at -/92.59%    2550
5   108nt, >ERR765432.2770540.1... at -/87.04%   2550

【问题讨论】:

  • 请显示两个输入文件和预期的结果输出文件 - 全部作为文本,而不是图像。谢谢。
  • 我按照您的建议进行了编辑。谢谢@MarkSetchell
  • 请问结果会怎样?
  • 我看不到预期的输出文件:-/
  • 我在问题的最后做了解释。好的,我将添加一个示例。

标签: python bash awk


【解决方案1】:
$ awk 'NR==FNR{map[$1]=$2; next} {print $0, map[$2]}' FS='\t' tab.txt FS='[>.]' OFS='\t' cd.txt
>Cluster 0
0   108nt, >ERR123456.1016542.1... *    1650
1   108nt, >ERR123456.3114223.2... at +/93.52%  1650
2   108nt, >ERR345678.217087.1... at -/89.81%   2350
3   108nt, >ERR345678.291581.2... at -/92.59%   2350
4   108nt, >ERR567890.3381351.2... at +/87.96%  1520
5   108nt, >ERR987654.126640.2... at -/86.11%   3960
6   108nt, >ERR987654.2492930.2... at +/84.26%  3960
7   108nt, >ERR987654.3327702.1... at +/92.59%  3960
>Cluster 1
0   108nt, >ERR876543.626414.2... *     4520
1   108nt, >ERR123456.3213598.2... at +/85.19%  1650
2   108nt, >ERR567890.1158706.2... at +/97.22%  1520
3   108nt, >ERR345678.146372.1... at -/88.89%   2350
4   108nt, >ERR765432.201531.2... at -/92.59%   2550
5   108nt, >ERR765432.2770540.1... at -/87.04%  2550

【讨论】:

  • 谢谢@Ed Morton。我认为它可能需要一些修改,因为它没有奏效。它在标准输出中显示输出,所以我在末尾添加了“>”以将输出定向到第三个文件,但它再次只显示了 CD-hit 文件。同样在匹配之后,您能告诉我如何将每个集群及其粘贴值提取到具有集群名称的地区文件中吗?
  • 编辑后它工作了:)。您有办法将集群解析为离散文件吗?
  • 看,你必须在你的问题中表明你要求做什么。这可以满足您的要求。如果您想做其他事情但没有显示,请使用它自己的minimal reproducible example 提出后续问题。
  • 很好。我会做的。感谢您的帮助@Ed Morton
猜你喜欢
  • 2021-10-16
  • 1970-01-01
  • 2021-02-27
  • 2015-05-12
  • 1970-01-01
  • 1970-01-01
  • 2023-03-06
  • 2016-12-29
  • 2013-10-20
相关资源
最近更新 更多