使用公共列合并文件(awk)

使用公共列合并文件(awk)

我有两个文本文件:

文件 1:

#CHROM  POS ID  REF ALT
1   61442   rs2531261   A   G
1   924629  rs28622096  A   G,T
1   974662  rs2465135   G   T
1   995481  rs9442393   T   G

文件 2

#CHROM  POS ID  REF ALT FORMAT
1   61442   rs2531261   A   G   GT:GL:GQ:DP:CGA_RDP:EHQ:HQ:CGA_CEGL:PS:CGA_CEHQ:AD
1   924629  rs28622096  A   G,T GT:GL:GQ:DP:CGA_RDP:EHQ:HQ:CGA_CEGL:PS:CGA_CEHQ:AD
1   974662  rs2465135   G   T   GT:GL:GQ:DP:CGA_RDP:EHQ:HQ:CGA_CEGL:PS:CGA_CEHQ:AD
1   987909  rs34235844  G   GTG GT:GL:GQ:DP:CGA_RDP:EHQ:HQ:CGA_CEGL:PS:CGA_CEHQ:AD
1   995481  rs9442393   T   G   GT:GL:GQ:DP:CGA_RDP:EHQ:HQ:CGA_CEGL:PS:CGA_CEHQ:AD

我想根据第 3 列 (ID) 合并文件。我的输出文件应如下所示:

输出:

#CHROM  POS ID  REF ALT FORMAT
1   61442   rs2531261   A   G   GT:GL:GQ:DP:CGA_RDP:EHQ:HQ:CGA_CEGL:PS:CGA_CEHQ:AD
1   924629  rs28622096  A   G,T GT:GL:GQ:DP:CGA_RDP:EHQ:HQ:CGA_CEGL:PS:CGA_CEHQ:AD
1   974662  rs2465135   G   T   GT:GL:GQ:DP:CGA_RDP:EHQ:HQ:CGA_CEGL:PS:CGA_CEHQ:AD
1   995481  rs9442393   T   G   GT:GL:GQ:DP:CGA_RDP:EHQ:HQ:CGA_CEGL:PS:CGA_CEHQ:AD

我想用awk

答案1

看起来你想筛选根据文件 1 中的 ID 查找文件 2(而不是合并文件):

$ awk 'NR==FNR {a[$3] = 1; next} $3 in a' file1 file2
#CHROM  POS ID  REF ALT FORMAT
1   61442   rs2531261   A   G   GT:GL:GQ:DP:CGA_RDP:EHQ:HQ:CGA_CEGL:PS:CGA_CEHQ:AD
1   924629  rs28622096  A   G,T GT:GL:GQ:DP:CGA_RDP:EHQ:HQ:CGA_CEGL:PS:CGA_CEHQ:AD
1   974662  rs2465135   G   T   GT:GL:GQ:DP:CGA_RDP:EHQ:HQ:CGA_CEGL:PS:CGA_CEHQ:AD
1   995481  rs9442393   T   G   GT:GL:GQ:DP:CGA_RDP:EHQ:HQ:CGA_CEGL:PS:CGA_CEHQ:AD

相关内容