AWK: Parsing a directed graph / Extracting records from a file based on fields matching parts of an input file
<h2>Problem</h2>

<p>So I have an input file with three fields. It is basically a list describing a type of directed graph. The first field is the starting node, second connection type (as in this case there is more than one), and last is the node projected onto. </p>

<p>The problem is, this is a very large and unruly direct graph, and I am only interested in some paths. So I want to provide an input file which has a name of nodes that I care about. If the nodes are mentioned in either the first or third field of the graph file, then I want that entire record (as the path type might vary).</p>

<h2>Question</h2>

<p>How to extract only certain records of a directed graph?</p>

<p>Bonus, how to extract only those paths which join nodes of interest by at most one neighbor (i.e. nodes of interest can be second nearest neighbors).</p>

<h2>Request</h2>

<p>I am trying to improve my AWK programming, which is why 1) I want to do this in AWK and 2) I would greatly appreciate a verbose explanation of the code :) </p>

<h2>Example of the Problem</h2>

<p>Input file:  </p>

<pre><code>A  
C  
D  
</code></pre>

<p>File to parse:</p>

<pre><code>A -&gt; B  
A -&gt; C  
A -&gt; D  
B -&gt; A  
B -&gt; D  
C -&gt; E  
D -&gt; F  
E -&gt; B  
E -&gt; F  
F -&gt; C  
</code></pre>

<p>... </p>

<p>Output:  </p>

<pre><code>A -&gt; B  
A -&gt; C  
A -&gt; D  
B -&gt; A   
B -&gt; D    
C -&gt; E   
D -&gt; F 
F -&gt; C
</code></pre>

<p>Bonus Example:  </p>

<pre><code> A -&gt; B -&gt; D  -&gt; F -&gt; C
</code></pre>