title: awk - send sum to global variable
body: <p>I have a line in a bash script that calculates the sum of unique IP requests to a certain page.</p>
  
  <p><code>grep $YESTERDAY $ACCESSLOG | grep "$1" | awk -F" - " '{print $1}' | sort | uniq -c | awk '{sum += 1; print } END { print "     ", sum, "total"}'</code></p>
  
  <p>I am trying to get the value of sum to a variable outside the awk statement so I can compare pages to each other. So far I have tried various combinations of something like this:</p>
  
  <p><code>unique_sum=0
  grep $YESTERDAY $ACCESSLOG | grep "$1" | awk -F" - " '{print $1}' | sort | uniq -c | awk '{sum += 1; print ; $unique_sum=sum} END { print "     ", sum, "total"}'
  echo "${unique_sum}"</code></p>
  
  <p>This results in an echo of "0". I've tried placing <code>__$unique_sum=sum__</code> in the <code>END</code>, various combinations of initializing the variable (<code>awk -v unique_sum=0 ...</code>) and placing the variable assignment outside of the quoted sections.</p>
  
  <p>So far, my Google-fu is failing horribly as most people just send the whole of the output to a variable. In this example, many lines are printed (one for each IP) in addition to the total. Failing a way to capture the 'sum' variable, is there a way to capture that last line of output?</p>
  
  <p>This is probably one of the most sophisticated things I've tried in awk so my confidence that I've done anything useful is pretty low. Any help will be greatly appreciated!</p>