shell - 使用映射文件更改列的名称
问题描述
我有一个包含 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命令。
解决方案
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
如果可能,您应该拆分第一个文件并在每个分区文件上并行运行此命令。然后,加入结果。
推荐阅读
- class - 结构成员名称是否比我使用继承的类成员使用更多内存?
- php - 带有 Laravel、Passport 和客户端凭据的私有推送通道授予客户端
- google-analytics - 分析报告 URL 中的唯一字符串是什么?
- xamarin - 如何使用 Xamarin 中的 NSDraggingInfo.EnumerateDraggingItems?
- jquery - 重置 .off() 方法
- r - 输出列表输入就绪,如 datapasta::dpasta
- excel - Excel 公式命名范围更改 - 2020 年 1 月更新
- python - 尝试将某些数据从二维数组复制到另一个数组时出错
- reactjs - React Native Expo - Jest - React Native Firebase - Invariant Violation:Native 模块不能为空
- django - Django urls 和过滤,实现这一点的正确方法