0

I have two tab-separated files:

File 1

123   456
135   567
234   478

File 2

123    notimportant    notimportant2
456    notimportant    notimportant2
987    notimportant    notimportant2
135    notimportant    notimportant2
234    notimportant    notimportant2
478    notimportant    notimportant2

I need to extract lines from file1 if both entries in a single row are present in file2's first column. So the output file should be like:

Output

123   456
234   478

I previously used this awk command to extract rows if only the first column of file1 matched 1st column of file2

awk 'FNR==NR{a[$1];next}($1 in a){print}' file2 file1

But I don't know how to expand it.

2
awk 'FNR==NR{a[$1]++;next;}($1 in a)&&($2 in a){print}' file2 file1

perl -lane '
   @ARGV and $h{$F[0]}++,next;
   print if @F == grep { $h{$_} } @F;
' file2 file1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.