Oh, I misunderstood your problem description. Here's something which hopefully is closer to what you wanted.
Code:
awk -F , -v acct=root 'NR==FNR { h[$1]++; next; }
{ if($1 == "") $1=host; host=$1; if (! h[$1]) next;
if ($2 == acct) print }' fileb filea
The
NR==FNR condition is true while
fileb is being read. This is a common
awk idiom for reading in a file of auxiliary data before the main processing. The host names in
fileb will be used to populate the array
h. Then in the main body of the script, if the first field is empty, the value of the
host variable will be used for
$1. Then the current value of $1 will be remembered in
host in case the next line(s) have empty first fields. Then, if the array
h does not contain the current host name (meaning it was not present in
fileb), the current line is skipped. Finally, if the second field is identical to the variable
acct, the line is printed.
(Really old variants of awk do not support passing in variables with the
-v option -- if you have this problem, see if you can find
nawk or
mawk or
gawk on your system, or an XPG4 awk. Or you can interpolate the variable directly into the script.)