I'm definitely interested in getting a book about awk. I was defeated, despite playing with him for a short time. However, at the same time, I have a problem that I suspect that I can only solve with [g] awk. To demonstrate, I will use some fdisk output. In this example, the desired end result will look something like this:
Disks: 2 [240 GB total]
sda=120 GB
sdb=120 GB
Here is 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 seems to print 73. on my laptop with two hard drives. I don’t get it. Bye, okay .. I may be halfway. Any advice or short tutorials will be greatly appreciated!
After two basic excellent answers, I got 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/: / /")
:
DISKS: 78.3585 GB total, 2 disks
sda 80.0 GB
sdb 4110 MB
, awk sed.:) , , !