awk - 用两个文件 awk 上的条件替换
问题描述
使用这些示例:
文件1:
rs12124819 1 0.020242 776546 A G
rs28765502 1 0.022137 832918 T C
rs7419119 1 0.022518 842013 T G
rs950122 1 0.022720 846864 G C
文件2:
1_752566 1 0 752566 G A
1_776546 1 0 776546 A G
1_832918 1 0 832918 T C
1_842013 1 0 842013 T G
如果它们的第 4 列相等,我正在尝试将 file2 的第 1 列更改为 file1 的相应第 1 列。
预期输出:
rs12124819 1 0 752566 G A
rs28765502 1 0 776546 A G
rs7419119 1 0 832918 T C
rs950122 1 0 842013 T G
我试图创建 2 个数组,但找不到正确的使用方法:
awk 'FNR==NR{a[$4],b[$1];next} ($4) in a{$1=b[FNR]}1' file1 file2 > out.txt
非常感谢!
解决方案
使用您显示的示例,您能否尝试以下操作。用 GNU 编写和测试awk
。
awk 'FNR==NR{a[$4]=$1;next} ($4 in a){$1=a[$4]} 1' file1 file2
说明:为上述添加详细说明。
awk ' ##Starting awk program from here.
FNR==NR{ ##Checking condition if FNR==NR which will be TRUE when file1 is being read.
a[$4]=$1 ##Creating array a whose index is $4 and value is $1.
next ##next will skip all further statements from here.
}
($4 in a){ ##Checking condition if 4th field is present in a then do following.
$1=a[$4] ##Setting value of 1st field of file2 as array a value with index of 4th column
}
1 ##1 will print edited/non-edited line.
' file1 file2 ##mentioning Input_file names here.
推荐阅读
- python - Why won't my tkinter window move further than 1 pixel?
- tfs - MSBuild is not installed on TFS controller when I installed VS 2017
- javascript - 如何使用 Javascript 从数据库中显示网站上的图像?
- ios - iOS 应用程序和地理编码的 Google Maps API 密钥限制
- typescript - Angular 中的模型
- html - 使用 css 选择器 jsoup 解析 html
- php - 如何使用 php 从 .ini 文件中删除部分
- typescript - 在 Yarn 工作区中使用 Typescript 引用时如何添加自定义类型定义?
- python - 如何增加散景中组标签之间的空间
- string - 查找第一个不同的字符串