bash - AWK/巴什 : how to match a field in one file from a field in another?

标签 bash shell file awk

我有 2 个文件,第一个包含以下内容:

...
John Allen Smith II 16 555-555-5555 10/24/2010
John Allen Smith II 3 555-555-5555 10/24/2010
John Allen Smith II 17 555-555-5555 10/24/2010
John Doe 16 555-555-5555 10/24/2010
Jane Smith 16 555-555-5555 9/16/2010
Jane Smith 00 555-555-5555 10/24/2010
...

第二个文件是一个名字列表,所以...

...
John Allen Smith II
John Doe
Jane Smith
...

是否可以使用 awk(或其他 bash 命令)打印第一个文件中与第二个文件中的任何名称匹配的行(名称可以在第一个文件中重复)

奖金?有没有一种简单的方法可以删除第一个文件中那些重复/重复的行?

非常感谢,

托梅克

最佳答案

#! /bin/bash
awk 'FNR==NR{!a[$0]++;next }{ b[$0]++ }
END{
  for(i in a){
    for(k in b){
      if (a[i]==1 && i ~ k ) { print i }
    }
  }
}' file1 file2

关于bash - AWK/巴什 : how to match a field in one file from a field in another?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45490359/

相关文章:

linux - 字符串中的 Shell 脚本参数

c - 在 C 中打印结构

c++ - 在 C++ 中以长度指示器方式从二进制文件中读取

file - 复制文件的简单方法

bash - 并行 grep 模式多个文件

bash - 错误的尾部语法或 grep 命令?

bash - 如何让 STDOUT 和 STDERR 都转到终端和日志文件?

linux - 如何检查用户是否存在于多个服务器的列表中?

linux - 我应该如何应用这种类型的补丁?

linux - Bash 输出流写入文件