Here’s an awk option, so you can avoid the bash dependency (for portability):
$ awk -F'|' 'NR==FNR{check[$0];next} $2 in check' file2 file1
How does this work?
-F'|'
— sets the field separator'NR==FNR{check[$0];next}
— if the total record number matches the file record number (i.e. we’re reading the first file provided), then we populate an array and continue.$2 in check
— If the second field was mentioned in the array we created, print the line (which is the default action if no actions are provided).file2 file1
— the files. Order is important due to theNR==FNR
construct.