Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'See battery percentage in Terminal and GeekTool' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
See battery percentage in Terminal and GeekTool
Authored by: lihtox on Feb 06, '10 06:34:06AM
awk '{printf("%.2f%%", $10/$5 * 100)}'

This first awk command reads the standard input, one line at a time (if necessary; I'm guessing only one line is being sent to it here), and splits the line into columns, the data in column c having the name $c. (So $5 is the 5th column's data and $10 is the 10th column's). The printf command calculates $10/$5*100 (that is, the 10th column divided by the 5th column, times 100) and prints it as a floating-point number with 2 decimal places ("%.2f") followed by a percentage sign ("%%").

awk '$3~/Capacity/ {c[$3]=$5} END{OFMT="%.2f%%"; max=c["\"MaxCapacity\""]; print(max>0?100*c["\"CurrentCapacity\""]/max:"?")}'

This awk command starts by reading in each line of ioreg -l, one at a time. Whenever column 3 ($3) contains the word "Capacity" ($3~/Capacity), it runs the command "c[$3]=$5", which stores the fifth column into an array (a box) labelled with the contents of the third column. (Unlike in some languages, awk's arrays can be labelled with text, not just numbers.)

The END segment means to run the following command after you're done reading in standard input. First it sets OFMT="%.2f%%", which means to change the output format so that it outputs real numbers with two decimal places followed by a percentage sign (as above). Second, it defines the variable max to be the contents of the array labelled "MaxCapacity" (which was defined in the first part of the process). Next it prints out the result of the formula max>0? 100*c["\"CurrentCapacity\""]/max: "?" which is an if-then statement (with the form test?true:false; awk stole this notation from C). If the variable max is greater than zero, then take the contents of the array labelled "CurrentCapacity", divide it by max, and multiply by 100. If max is not greater than zero, then something wrong happened (maybe ioreg doesn't have a line which says "MaxCapacity") and so return the character "?".

Type "man awk" in the terminal for a full description of the command. It's basically a full-blown programming language, designed to deal with text files one line at a time, and it's really useful; it's also something you can pick up a little bit at a time, if you're so inclined.

[ Reply to This | # ]

See battery percentage in Terminal and GeekTool
Authored by: mael on Dec 15, '10 02:42:11AM

I just realized I've never thanked you for explaining the commands.
I've (at the time) just started to work with awk and your detailed breakdown helped a lot in understanding.
People like you make this site to what it is!
So, some months late but from my heart ... THANK YOU!


In the meantime I've not only understood what awk does, but could also adapt it to a different ioreg-syntax, thus speeding up the command by a factor of 15 (thats a speed increase of 1500% - well worth a try).

50 iterations of the original command took a 15.3 secs on a MacBook (not Pro) Unibody.
The same number of iterations with the modified line take only 1.1 sec...:

ioreg -n AppleSmartBattery -r | awk '$1~/Capacity/{c[$1]=$3} END{OFMT="%.2f%%"; max=c["\"MaxCapacity\""]; print (max>0? 100*c["\"CurrentCapacity\""]/max: "?")}'

Edited on Dec 15, '10 03:09:14AM by mael



[ Reply to This | # ]