根据另一个文件作为匹配条件过滤文件条目 [英] Filtering file entries based on another file as match condition

查看:83
本文介绍了根据另一个文件作为匹配条件过滤文件条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

bash和脚本编写的真正初学者,如果对您来说这是一个非常基本的问题,请对不起.

Really beginner of bash and scripting so sorry if it is very basic question to You.

我有大约1百万行的file1,每行包含两个字段. 我有〜270.000行的file2,每行有一个条目.与file1字段1相同.

I have file1 with ~1 million rows contain two fields in every row. I have file2 with ~270.000 rows, single entry in every rows. It is common with file1 field 1.

目标是基于file2条目从file1(保留filed1和field2条目)中筛选出一个列表.

The goal is to have a filtered list from the file1 (keep the filed1 and field2 entries) based on file2 entries.

示例:

file1

1 A

2 B

3 C

4 C

5 D

6 A

7 G

8 K

122 F

.

.

56677 A

.

7272727272 A

1.000.000 A

File2:

1

2

3

9

122

56677

7272727272

我要基于file2过滤第一列,输出应如下所示:

I want filter the first column based on file2 and output should be like this:

1 A

2 B

3 C

122 F

56677 A

7272727272 A

推荐答案

如果提供了预期的输出,请尝试以下行:

try this line, if it gave expected output:

grep -Fwf file2 file1

awk 'NR==FNR{a[$0]=1;next}a[$1]' file2 file1

这篇关于根据另一个文件作为匹配条件过滤文件条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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