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/3947799/

相关文章:

bash - 使用 shell 脚本从文件中提取一行

bash - 字符串封装如 $"Hello World."

bash - 在我的 ~/.bashrc 文件中创建 "local"别名后自动填充失败

file - 删除EMPTY目录或文件夹/子文件夹

c++ - 读取.txt文件并组织成二维数组

linux - 创建同名文件和目录

linux - 查找并删除早于 x 天的文件或文件夹

bash - 计算一列中具有特定符号的行数(来自 grep 的结果)?

linux - 需要将输出重定向到/dev/null....在命令行中工作正常但在 shell 中不工作

file - 如何使用歌词解释数据压缩