You are basically trying to use the second file as an index. You could filter the file first, to keep only the wanted lines, and then process the result line-by-line.
It's probably easier to build an awk array of line numbers to process.
BEGINawk 'NR==FNR { array_len=0array[FNR] = $1 } NR!=FNR { array[array_len++]file1_lines[FNR] = $1$0 } END { for(i=0;i<array_len;i++linenum in array) { do stuffprint withfile1_lines[array[linenum]] array[i]th} line}' offile2 file1 } } You can redirect this to another file and then process the whole of it without filtering.