title: AWK: print columns, sum of column, & possible missing column
body: <p><strong>For a little background:</strong> I have output from the jms ems admin tool reporting connections to a JMS queue.  I currently filter out the 5th, 6th, and NF-1 columns (needed as the 7th column is not always present) and summing the NF-1 column based upon the 5th column, using this statement:</p>
  
  <pre><code>echo $input|awk '$4 = "+" {print $5, $(NF-1), $6}'|awk '{a[$1]+=$2;b[$1]=$3}END{for (i in a){print "           "a[i]"\t",b[i]"\t"i}}'
  </code></pre>
  
  <p>While I'm sure you know what I'm doing, I'll break down the thought process.</p>
  
  <ol>
  <li>Input entering from the source and piped in</li>
  <li>Check that the 4th filed is a plus sign and print the fields I want (mostly for division of labor and I can test input on Step 3</li>
  <li>Sum column 2 (original NF-1) based upon column 1 (original 5th field) and print the rest</li>
  </ol>
  
  <p><strong>Example input:</strong></p>
  
  <pre><code>J  553386752 +--Q  +  vm1868            utsl099                                                                                                                                                                                 1   10:39:47
  J  553386804 +--Q  +  vm1868            utsl099                                                                                                                                                                                 4   10:39:44
  J  553386815 +--Q  +  vm1868            utsl099                                                                                                                                                                                 4   10:39:47
  J  553386851 +--Q  +  vm1868            utsl099                                                                                                                                                                                 4   10:39:47
  J  553386895 +--Q  +  vm1868            utsl099                                                                                                                                                                                 4   10:39:41
  J  553386931 +--Q  +  vm1868            utsl099                                                                                                                                                                                 4   10:39:47
  J  553386932 +--Q  +  vm1868            utsl099                                                                                                                                                                                 4   10:39:47
  J  553391311 +--T  -  vm1657            ueai099    cn=ubai090,ou=domain,o=bn,udi=bai-event-mover-3_0-vm165d-prod                                                                                       1   10:39:44
  J  553433995 +--Q  +  vm1479            usln099                                                                                                                                                                                 5   10:39:50
  J  553433996 +--Q  +  vm1479            usln099                                                                                                                                                                                 5   10:39:50
  J  553433997 +--Q  +  vm1479            usln099                                                                                                                                                                                 5   10:39:50
  J  553435753 +--Q  +  vm1479            ubrm099    some-other-program-name-1_0-prod-vm1454-2014-08-12T15:31:59.541Z.vm1454                                                                             4   10:39:44
  J  553435927 +--T  +  vm1479            ubrm099    ${JMS_CLIENT_ID}.admin.vm1433.2014-08-12T15:32:26.743Z                                                                                                                       1   10:39:50
  </code></pre>
  
  <p><strong>What I would desire for output:</strong></p>
  
  <pre><code>[sum of NF-1 based upon the uniqueness of $5 AND $7 (if it exists)]   [$6]   [$5]   [$7]
  </code></pre>
  
  <p><strong>which would look like:</strong></p>
  
  <pre><code>#   user     server  program (clientID)
  25  utsl099  vm1868
  15  usln099  vm1479
  4   ubrm099  vm1479  some-other-program-name-1_0-prod-vm1454
  1   ubrm099  vm1479  ${JMS_CLIENT_ID}.admin.vm1433
  </code></pre>
  
  <p><strong>The obstacles would be:</strong> </p>
  
  <ol>
  <li>IF $7 == $NF-3, then the intermittent field DOES exist and should be printed</li>
  <li>SUM based upon the possible existence of the field AND $5</li>
  </ol>
  
  <p>If there is a better way to do this, I am open to suggestions.  I feel this is a challenge, but figured more minds to it would help, as I am not an awk expert. Thank you in advance.</p>