<p>I'm processing a huge file with (GNU) <code>awk</code>, (other available tools are: Linux shell tools, some old (>5.0) version of Perl, but can't install modules).</p>

<p>My problem: if some field1, field2, field3 contain X, Y, Z I must search for a file in another directory which contains field4, and field5 on one line, and insert some data from the found file to the current output.</p>

<p>E.g.:</p>

<p>Actual file line:</p>

<pre><code>f1 f2 f3 f4 f5
X  Y  Z  A  B
</code></pre>

<p>Now I need to search for another file (in another directory), which contains e.g.</p>

<pre><code>f1 f2 f3 f4
A  U  B  W
</code></pre>

<p>And write to STDOUT <code>$0</code> from the original file, and <code>f2</code> and <code>f3</code> from the found file, then process the next line of the original file.</p>

<p>Is it possible to do it with <code>awk</code>?</p>

This seems to work for some test files I set up matching your examples.  Involving perl in this manner (interposed with grep) is probably going to hurt the performance a great deal, though...
```
```
## perl code to do some dirty work

for my $line (`grep 'X Y Z' myhugefile`) {
    chomp $line;
    my ($a, $b, $c, $d, $e) = split(/ /,$line);
    my $cmd = 'grep -P "' . $d . ' .+? ' . $e .'" otherfile';
    for my $from_otherfile (`$cmd`) {
        chomp $from_otherfile;
        my ($oa, $ob, $oc, $od) = split(/ /,$from_otherfile);
        print "$a $ob $oc\n";
    }
}
```
```
EDIT:
Use tsee's solution (above), it's much more well-thought-out.