AWK/BASH:如何将一个文件中的一个字段与另一个文件中的一个字段进行匹配? [英] AWK/BASH: how to match a field in one file from a field in another?

查看:349
本文介绍了AWK/BASH:如何将一个文件中的一个字段与另一个文件中的一个字段进行匹配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have 2 files, the first contains the following:

...
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
...

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

and the second file is a list of names so...

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

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

Is it possible to use awk (or other bash command) to print the lines in the first file that match any name in the second file (the names can repeat in the first file)

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

Bonus? Is there an easy way to remove those repeated/duplicate lines in the first file?

非常感谢

托莫克

推荐答案

awk

#! /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

这篇关于AWK/BASH:如何将一个文件中的一个字段与另一个文件中的一个字段进行匹配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆