Filtering file entries based on another file as ma

2019-09-01 02:56发布

问题:

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