Here is one solution:
awk -F_ 'NR==FNR {x[$2]=$0;a[$2]=1;next} {y[$2]=$0;a[$2]=1} END {for (i in a) print x[i] "\t" y[i]}' file1 file2
x_a y_a
x_b
y_c
x_d
x_e y_e
And with ???
awk -F_ 'NR==FNR {x[$2]=$0;a[$2]=1;next} {y[$2]=$0;a[$2]=1} END {for (i in a) print (x[i]?x[i]:"???"),(y[i]?y[i]:"???")}' file1 file2
x_a y_a
x_b ???
??? y_c
x_d ???
x_e y_e
How does it work?
awk -F_ ' # Field separator set to _
NR==FNR { # Run this on first file
x[$2]=$0 # Store line in array x, using field 2 as separator
a[$2]=1 # Store every unique separator in array a
next # Next record
}
{ # Run this on second file
y[$2]=$0 # Store line in array y, using field 2 as separator
a[$2]=1 # Store every unique separator in array a
}
END { # Do this at end
for (i in a) # Read all unique value in array a
print x[i] "\t" y[i] # Print the array x and y using value in a
}' file1 file2 # Read the files