Filtering file entries based on another file as match condition

若如初见. 提交于 2019-12-11 19:48:52

问题


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

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.

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

Example:

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

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

回答1:


try this line, if it gave expected output:

grep -Fwf file2 file1

or

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


来源:https://stackoverflow.com/questions/22837707/filtering-file-entries-based-on-another-file-as-match-condition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!