Home > Blockchain >  How to extract rows in a file based on common information in another file?
How to extract rows in a file based on common information in another file?

Time:05-03

I have file look like: file1:

8189157 7423700 7227471
8189199 7533538 7574425
8189200 7533538 7574425
8189273 7241932 7538338
8191298 6353935 6770138
8191332 7427024 7756709
8192601 6353935 7378544
8192680 7533538 7574348
8193100 6678109 7755961
8193158 6678109 7367734
8193159 6678109 7367734
8193176 7427024 7377679
8193180 7427024 7377679
8193227 6678109 7347206
8207305 7427024 7575134
8207315 6353935 7767680
8207316 6353935 7767680
8207317 6353935 7767680
8207371 6678109 7793130
8209083 7533538 7426859
8212702 7268724 7367752
8212704 7268724 7367752
8212718 7753798 7575212
8212719 7753798 7575212

I want to extract all the rows from file1 which have a common value with file 2:

7753798
6353935
7423700

so the result should be a third file like:

8212718 7753798 7575212
8212719 7753798 7575212
8207315 6353935 7767680
8207316 6353935 7767680
8207317 6353935 7767680
8191298 6353935 6770138
8192601 6353935 7378544
8189157 7423700 7227471

Any suggestions please by considering the fact that the real file1 is huge.

Thanks

CodePudding user response:

Suggesting awk script:

Notice order of files is important

awk '
FNR==NR{words[  cnt] = $0;next}  # read search words from file2
{ # for each line in file1
  for (i in words){ # scan each words
    # if word is matched, print line and process next line
    if ($0 ~ words[i]) {print; next}
  }
}
' file2 file1
  • Related