I cannot seem to get what should be a simple awk one-liner to work correctly and cannot figure out why. I would like to use patterns from a specific field in one file as regex to search for matching strings in the entire line ($0) of another file.
I would like to output the lines of File2 which contain the string of $2 in File1.
File1
PS002,002 RZN ? 0 1 0 0 1 4 -1 6 0 3 2 2 2 2 1 -1 -1 -1 0 502 0
PS003,001 BRX ? 0 1 1 0 1 1 -1 4 0 0 0 1 1 1 -1 -1 -1 -1 0 501 0
PS006,009 P<L ? 0 1 0 0 1 5 -1 6 0 3 2 1 2 0 -1 2 -1 -1 -1 -1 -1
PS007,001 CJR= ? 0 1 0 0 1 -1 -1 2 3 1 2 -1 1 1 -1 -1 -1 -1 0 501 0
PS017,003 ZMM ? 0 1 0 0 6 -1 -1 2 1 1 0 -1 1 1 -1 -1 -1 -1 0 501 0
PS017,004 CMR ? 0 1 0 0 6 -1 -1 2 1 1 0 -1 1 1 -1 -1 -1 -1 0 501 0
PS018,001 >JB ? 0 1 0 0 1 5 -1 6 0 3 2 1 2 0 -1 306 -1 -1 -1 -1 -1
PS018,002 >MR ? -1 1 2 0 1 -1 -1 11 3 1 2 -1 1 1 -1 -1 -1 -1 0 501 0
PS018,018 FN> ? 0 1 0 0 1 5 -1 6 0 3 2 1 2 0 -1 -1 -1 -1 -1 -1 -1
File2
PS003,001 MZMWR/ L-DWD// *
PS003,001 B-!!BRX[/+W M(N-PN(H/J >BCLWM// BN/+W *
PS004,001 L-(H-1M]]NYX[/ B-NGJN(H/WT MZMWR/ L-DWD// *
PS016,001 MKTM/ L-DWD// *
PS017,001 TPL(H/H L-DWD// *
PS018,001 L-(H-1M]]NYX[/ L-<BD/ JHWH// L-DWD// >CR ]]DBR[ L-JHWH// >T DBR/J H-CJR(H/H H-Z>T *
PS018,001 B-JWM/ ]H](NY1JL[ JHWH// >1WT+W M(N-KP/ KL/ >JB[/J+W W-M(N-JD/ C>WL// *
PS019,001 L-(H-1M]]NYX[/ MZMWR/ L-DWD// *
Desired Output:
(These two lines contain the strings BRX and >JB from $2 of File1)
PS003,001 B-!!BRX[/+W M(N-PN(H/J >BCLWM// BN/+W *
PS018,001 B-JWM/ ]H](NY1JL[ JHWH// >1WT+W M(N-KP/ KL/ >JB[/J+W W-M(N-JD/ C>WL// *
It seems to me that either of the two following awk one-liners should do the trick:
awk 'NR==FNR{A[$0]++;next}($2 in A){print A[$0]}' File2 File1
awk 'NR==FNR{A[$1]=$0;next}$2 in A{print A[$1]}' File2 File1
I have even attempted to name all of the records of File2 in a variable for use in the print statement
awk 'NR==FNR{x=$0; A[x];next}$2 in A {print x}' File2 File1
However, these keeps returning nothing.
Nevertheless I can get it to work with grep:
grep -f <(awk '{print $2}' File1) File2
I would like to get the forum's help as to why my awk code is failing.