awk - awk:打印与文件中的模式不匹配的行,查看特定列
问题描述
我有一个idFile
:
1006006
1006008
1006011
1007002
......
和famFile
:
1006 1006001 1006016 1006017 1
1006 1006006 1006016 1006017 1
1006 1006007 0 0 2
1006 1006008 1006007 1006006 2
1006 1006010 1006016 1006017 2
1006 1006011 1006016 1006017 1
1006 1006016 0 0 2
1006 1006017 0 0 1
1007 1007001 1007950 1007015 2
1007 1007002 1007014 1007015 2
......
我需要 grepfamFile
中第二列与idFile
.
awk 'BEGIN { while(getline <"idFile") id[$0]=1; }
id[$2] ' famFile
返回所有匹配项:
1006 1006006 1006016 1006017 1
1006 1006008 1006007 1006006 2
1006 1006011 1006016 1006017 1
1007 1007002 1007014 1007015 2
......
但是我怎样才能修改命令来获得匹配的补码呢?
解决方案
$ awk 'NR==FNR{a[$1];next} !($2 in a)' idFile famFile
1006 1006001 1006016 1006017 1
1006 1006007 0 0 2
1006 1006010 1006016 1006017 2
1006 1006016 0 0 2
1006 1006017 0 0 1
1007 1007001 1007950 1007015 2
解释:
$ awk '
NR==FNR { # process the idFile
a[$1] # hash to a
next # next id
}
!($2 in a) # if the second field id is not in a, output record
' idFile famFile # mind the file order
推荐阅读
- rust - 如何错误处理字符串到浮点数的转换
- python - Pandas read_html() 没有在表格中给出我的所有条目
- c++ - 将指向任何类型的指针转换为用于算术的 char 指针是否安全且可移植?
- php - WP 管理员 ajax 返回 404
- postgresql - 如何使用 Circe 从 Doobie Scala PostgreSQL 数据库中读取 JSON?
- firebase - Firebase 身份验证上的 Flutter 应用程序错误
- windows-subsystem-for-linux - 如何将 vdhx 文件注册为 WSL 2?
- sql - SQL 唯一标识符到 varchar
- c# - 窗口窗体最顶层有什么区别?C#
- spring-boot - 在 Apache Kafka java 中实现请求响应