首页 > 解决方案 > 使用映射文件更改列的名称

问题描述

我有一个包含 3 列的文件,如下所示:

NC_0001 10 x
NC_0001 11 x
NC_0002 90 y

我想使用另一个包含转换的文件 .txt 更改第一列的名称,就像:

NC_0001 1
NC_0001 1
NC_0002 2

...

所以最后我应该有:

1 10 x
1 11 x
2 90 y

我怎样才能做到这一点?PS第一个文件非常大(50 GB)所以我必须使用像awk这样的unix命令。

标签: shellfiledictionaryunixawk

解决方案


awk -f script.awk map_file data_file
NR == FNR {                  # for the first file
    tab[$1] = $2             # create a k/v of the colname and rename value
}

NR != FNR {                  # for the second file
    $1 = tab[$1]             # set first column equal to the map value
    print                    # print
}

作为单行

awk 'NR==FNR{t[$1]=$2} NR!=FNR{$1=t[$1];print}' map_file data_file

如果可能,您应该拆分第一个文件并在每个分区文件上并行运行此命令。然后,加入结果。


推荐阅读