awk - awk 中的匹配
问题描述
请问这有什么问题?
awk 'NR == FNR { x[$1]=$1; next} { print x[$5], $0 }' A.dat B.txt > C.txt
我想:
如果文件 A.dat 的第一列等于文件 B.txt 的第一列,则打印文件 A.dat 的第五列和文件 B.txt 的其余部分
数据
50551.4765 7.8001 7.8001 1.0000 7
50458.7628 7.3914 7.3914 1.0000 7
50134.5127 4.4601 4.4601 1.0000 4
50476.5679 7.2830 7.2830 1.0000 7
50102.5924 4.5385 4.4912 1.0105 4
数据
50102.5924 72.220
50103.5903 -3.800
50107.5850 -23.670
50108.5331 40.380
50108.7620 -7.620
50109.5345 75.810
50109.7681 54.510
期望的结果 C.txt
4 50102.5924 72.220
解决方案
使用您显示的示例,请尝试以下代码。
awk 'FNR==NR{arr[$1]=$NF;next} ($1 in arr){print arr[$1],$0}' A.dat B.dat
解释:
awk ' ##Starting awk program from here.
FNR==NR{ ##Checking condition if FNR==NR which will be TRUE for A.dat
arr[$1]=$NF ##Creating arr with index of $1 and which has value of last field.
next ##next will skip all further statements from here.
}
($1 in arr){ ##Checking condition if 1st field is present in arr then do following.
print arr[$1],$0 ##Printing arr with index of $1 and current line.
}
' A.dat B.dat ##Mentioning Input_file names here.
推荐阅读
- javascript - ng new 上的“工作区需要在使用前加载”
- android - Android 和 iOS 是否能够在没有应用程序的情况下为手电筒等外部设备供电?
- javascript - Babylon.js 加载 MTL 的问题。文件到 OBJ。从 Blender 导出的模型
- mysql - 我可以在一个实例上为每个架构设置多个从属主机吗?
- python - 如何根据日期范围从字典列表中过滤出子字典
- r - 找不到功能,但是包肯定是安装加载的
- r - 减去并找到值或体积的差异
- html - iframe 后面的下拉菜单?
- j - 从时间设置RNG种子
- asp.net-core - 使用 Azure DevOps 将 Asp.Net Core 应用程序部署到 Azure 时未找到包