-1

I have a large text file (file A) as below:

ID1 ID2 0.2
ID3 ID6 0.4
ID4 ID2 0.6
ID5 ID8 0.8
ID7 ID14    0.4
ID3 ID9 0.6 
ID8 ID10 0.8

I have also two another text files (B and C), which file B is like below:

ID1
ID2
ID3
ID4
ID5
ID6
ID7

and file C is like:

ID8
ID9
ID10
ID11
ID12
ID13
ID14

I would like to keep just those row of the file A that the related contents of two first columns belong to both file B and file C. For example, I want just these rows from file A:

ID10 ID3    0.8
ID7 ID14    0.4
ID3 ID9 0.6

Because the ID 10 and ID3 belonged to both two files of B and C, and so on. Could you please kindly let me know how to obtain such an output?

Many thanks

1
  • What about ID5 ID8 0.8? And ID10 ID3 0.8 does not occur in the data.
    – Kusalananda
    Apr 25, 2020 at 7:14

1 Answer 1

1

Simply grep the A file for strings occurring in the B file, and then grep the result of that for strings occurring in the C file.

$ ( grep -wF -f fileB | grep -wF -f fileC ) <fileA
ID5 ID8 0.8
ID7 ID14    0.4
ID3 ID9 0.6

The options used with grep are

  • -w to only ever match whole words (this is a commonly available, but non-standard option). We use this option to not match ID10 when grepping for ID1.
  • -F to interpret the patterns as strings, not as regular expressions.
  • -f to read the patterns to match with from a separate file.
2

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .