awk - compare different column of 2 different files and keep the orderin awk script
问题描述
I am trying to compare 2 different files ex:
file 1:
CALL AA
CALL AB
CALL AC
file 2:
00 AB n
01 AA o
02 AC o
the result i expect is output:
AA 01 o
AB 00 n
AC 02 o
How can i get this without changing the order of the first file?(preferably in awk) Thank you in advance.
解决方案
从上面链接的示例更改
awk 'NR==FNR {x=$1; $1="";a[x]=$0; next} {print $0,a[$3]}' Sourceports NATLog
至
awk 'NR==FNR {x=$2; $2="";a[x]=$0; next} {print $2,a[$2]}' file2 file1
输出:
AA 01 o AB 00 n 交流电 02 o
通过优化:
awk 'NR==FNR {a[$2]=$2 FS $1 FS $3; next} {print a[$2]}' file2 file1
现在输出没有空列:
AA 01 o AB 00 n 交流电 02 o
推荐阅读
- python - 如何在电子邮件正文内的列表中通过电子邮件发送值
- c# - 使用 Bot Framework 将文件上传给用户
- swift - 在 iOS 11.4 上隐藏导航返回按钮
- c# - 如何将整数值传递给命令参数 MultiBinding
- python - 使用 Tensorflow 查找和使用输入的损失梯度
- python - Python在运行时记录新的热键
- eclipse - macOS 上传统 8.5 的 Websphere 开发人员工具 - 无法连接到在 Docker 上运行的 WAS
- android-layout - 通过 TabLayout/ViewPager 使用 Fragments 的可重用布局
- r - 如何使用 R 中的 bootstrap 方法计算 beta 回归拟合值的置信区间
- c++ - C++ 运算符重载 >> << ()