简体   繁体   English

比较两个文件diff awk else

[英]comparing two files diff awk else

Could you please quickly help me with this? 您能尽快帮我吗? I have two files with 1 columns in each file. 我有两个文件,每个文件有1列。 I need to compare fileA to fileB and find out which items in FileA are already in FILEB and print them out to another file.So basically like to find out which name they have in common. 我需要将fileA与fileB进行比较,找出FileA中的哪些项目已经存在于FILEB中,然后将它们打印到另一个文件中,因此基本上想找出它们的共同名称。

so I have something like this 所以我有这样的事情

FILEA

MATT.1
HANNA.1
OTTOO.2
MARK.2
SAM.3

FILEB

SAM.3
MATT.1
JEFF.6
ALI.8

The result file should be 结果文件应为

SAM.3
MATT.1

I was thinking of writing a shell script cat one file and do a line by line comparison, but there must a better and easier way to do this using one of many commands. 我当时正在考虑编写一个shell脚本cat一个文件并进行逐行比较,但是必须有一种更好,更轻松的方法来使用许多命令之一来执行此操作。 Can you help? 你能帮我吗?

Regards 问候

This is a job for comm . 这是comm的工作。 The input files need to be sorted though 输入文件需要排序

comm -12 <(sort file1) <(sort file2)

will give you the common lines. 会给你一些共同点。

An awk answer: 一个awk的答案:

awk 'NR==FNR {f[$0]=1; next} $0 in f' fileb filea

Put the smaller file as the first argument to limit the amount of memory required. 将较小的文件作为第一个参数,以限制所需的内存量。

This looks returns lines from filea that match any line in fileb: 这看起来从filea返回的行与fileb中的任何行匹配:

$ grep -Ff fileb filea
MATT.1
SAM.3

-F tells grep to look for fixed patterns, not regular expressions. -F告诉grep查找固定模式,而不是正则表达式。

-f tells grep to get the list of patterns from a file which, in this case, is fileb . -f告诉grep从文件(在本例中为fileb获取模式列表。

More options 更多选择

We can make the matches more restrictive with these options: 我们可以使用以下选项使比赛更具限制性:

-w would tell grep to match only whole words. -w会告诉grep仅匹配整个单词。

-x would tell grep to match only whole lines. -x会告诉grep只匹配整行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM