NR is the total number of records processed (inclusive of the one being processed currently);
FNR is the number of records processed in the current file. This will only happen for the first file.
if (split($1,a, "_") ==3)
NF is the number of fields in the current record. If it is a blank line, this will be zero, which is "falsy" (as opposed to "truthy"), and so this block will only execute for non-blank lines. Make a note of the code in this block where the value of
p is set; that will be important later.
ORS is the Output Record Separator; this is what separates each record as it is written out. By default is is a newline. Here is is conditionally used as the value for the
if (split($1, a, "_") ==3 && $1 in f1)
rec=(!rec)?$0:rec ORS $0
This last block will run only if the value of
p is "truthy".
print rec ORS