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:
Disks: 2 [240 GB total]
sda=120 GB
sdb=120 GB
Here's what I have:
fdisk -l 2>/dev/null | awk '/^Disk \/dev\/[vsh]d./ {bytes+=$5} END {print "Disks: "NR" ["; gb=bytes/1024/1024/1024; print gb" GB total]"}'
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!
After two major excellent answers, I ended up with the following:
echo DISKS: $(fdisk -l 2>/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/: / /")
Which gave me output like this:
DISKS: 78.3585 GB total, 2 disks
sda 80.0 GB
sdb 4110 MB
Looking forward to the day I can do that all in one awk command with no sed. :) But for now, thanks folks!