【问题标题】:Matching two files and print all columns匹配两个文件并打印所有列
【发布时间】:2021-08-23 15:53:44
【问题描述】:

我有两个文件要根据文件 1 中的第 1 列和文件 2 中的第 2 列进行匹配。

文件 1:

1000019 -0.013936       0.0069218       -0.0048443      -0.0053688
1000054 0.013993        0.0044969       -0.0050022      -0.0043233  

文件 2:

5131885 1000019
1281471 1000054

我想在匹配后打印所有列。 预期输出(文件 3):

5131885 1000019 -0.013936       0.0069218       -0.0048443      -0.0053688
1281471 1000054 0.013993        0.0044969       -0.0050022      -0.0043233 

我尝试了以下方法:

awk 'FNR==NR{arr[$1]=$2;next} ($2 in arr){print $0,arr[$2]}' file1 file2 > file3
join file1 file2 > file3 #after sorting

【问题讨论】:

  • 尝试加入 -1 1 -2 2 file1 file2 或类似的东西。 linux.die.net/man/1/join
  • 我用预期的输出编辑了帖子。
  • 加入 -1 1 -2 2 file1 file2 -o 2.2,2.1,1.2,1.3,1.4,1.5

标签: linux join awk


【解决方案1】:

这个awk 应该可以工作

awk 'NR==FNR {r[$2]=$1; next}{print r[$1], $0}' $file2 $file1

输出

5131885 1000019 -0.013936       0.0069218       -0.0048443      -0.0053688
1281471 1000054 0.013993        0.0044969       -0.0050022      -0.0043233

【讨论】:

    猜你喜欢
    • 2018-07-12
    • 2018-08-08
    • 1970-01-01
    • 2016-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-05
    • 2020-11-24
    相关资源
    最近更新 更多