title: Compare 2 files with awk and substract value from one column if the value of another column matches in both files
body: <p>I am trying to compare to files on an Ubuntu system. Both files have the same two column structure, and if the value of column two match for a given line subtract the values of column 1 for the same line.</p>
  
  <p>I have tried a couple of things, the closest I got is with this command : </p>
  
  <pre><code>awk 'NR==FNR{a[$1]=$1;b[$2]=$2;next} {if ($2 in b) print $1-a[$1]," ",$2}' file1 file 2
  </code></pre>
  
  <p>But I guess I have something wrong since the result seems to acknowledge only lines that fill the condition where the second column match but allows only lines that have the same value for the first column.</p>
  
  <p>For example:</p>
  
  <p><strong>File1 :</strong> </p>
  
  <pre><code>10 A
  20 B
  30 C
  10 D
  </code></pre>
  
  <p><strong>File2 :</strong> </p>
  
  <pre><code>10 A
  20 E
  20 F
  50 D
  </code></pre>
  
  <p><strong>Should return :</strong> </p>
  
  <pre><code>0 A
  -40 D
  </code></pre>
  
  <p><strong>Returns :</strong> </p>
  
  <pre><code>0 A
  </code></pre>