title: awk-1-liner to sum numbers in stdin, while printing number of occurrences of certain text along with other arbritrary text
body: <p>I'm definitely interested in getting a book on awk. I've been won over, despite playing with it for little time. However, in the mean time, I have a problem I suspect I could solve exclusively with [g]awk. To demonstrate, I'll use some fdisk output. In this example, the desired end result would be something like:</p>
  
  <pre><code>Disks: 2 [240 GB total]
  sda=120 GB
  sdb=120 GB
  </code></pre>
  
  <p>Here's what I have:</p>
  
  <pre><code>fdisk -l 2&gt;/dev/null | awk '/^Disk \/dev\/[vsh]d./ {bytes+=$5} END {print "Disks: "NR" ["; gb=bytes/1024/1024/1024; print gb" GB total]"}'
  </code></pre>
  
  <p>My NR apparently prints out 73.. on my laptop with two hard drives, anyway. This, I do not understand. So far, well.. I'm maybe half-way there. Any tips or succinct tutorials would be greatly appreciated!</p>
  
  <hr>
  
  <p>After two major excellent answers, I ended up with the following:</p>
  
  <pre><code>echo DISKS: $(fdisk -l 2&gt;/dev/null | awk -F/ '/^Disk \/dev\/[vsh]d.|^Disk \/dev\/xvd./{print$3}' | awk '{d++;bytes+=$4;disk[$1]=$2" "$3}END{gb=bytes/1024/1024/1024; printf gb" GB total, "d" disks\n";for (i in disk) print "  "i,disk[i]}' | sed -e "s/,$/ /" -e "s/: / /")
  </code></pre>
  
  <p>Which gave me output like this:</p>
  
  <pre><code>DISKS: 78.3585 GB total, 2 disks
    sda 80.0 GB
    sdb 4110 MB
  </code></pre>
  
  <p>Looking forward to the day I can do that all in one awk command with no sed. :) But for now, thanks folks!</p>