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

See battery percentage in Terminal and GeekTool System 10.6
I have been building up a number of commands that I can use in GeekTool. One that I wanted but couldn't find in other lists was the current battery capacity. This works for me:
ioreg -l | grep -i capacity | tr '\n' ' | ' | awk '{printf("%.2f%%", $10/$5 * 100)}'
[robg adds: This worked as shown on my 10.6.2 on my MacBook Pro. To get it work in 10.5, though, I had to modify the command a bit; I found that this version works in 10.5:
ioreg -l | grep -i capacity | grep -v Legacy| tr '\n' ' | ' | awk '{printf("%.2f%%", $14/$7 * 100)}'
Note that the output above is set up for GeekTool, where you don't want a newline. If you want to run this in Terminal, you can change the printf bit (in either version) to read ...2f%%\n..., which will add a line break at the end.]
    •    
  • Currently 2.80 / 5
  You rated: 5 / 5 (15 votes cast)
 
[24,864 views]  

See battery percentage in Terminal and GeekTool | 12 comments | Create New Account
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: googoo on Feb 01, '10 10:26:30AM
I used the following to add a linebreak after the command output:

ioreg -l | grep -i capacity | tr '\n' ' | ' | awk '{printf("%.2f%%\n", $10/$5 * 100)}'

I added a \n after the last % in the printf command.

-Mark



[ Reply to This | # ]
See battery percentage in Terminal and GeekTool
Authored by: wleahcim on Feb 01, '10 11:01:37AM
This can be improved to use less processes:
ioreg -l | awk '$3~/Capacity/{c[$3]=$5}END{OFMT="%.2f%%";max=c["\"MaxCapacity\""];print(max>0?100*c["\"CurrentCapacity\""]/max:"?")}'
Edited on Feb 01, '10 11:02:23AM by wleahcim


[ Reply to This | # ]
See battery percentage in Terminal and GeekTool
Authored by: mael on Feb 02, '10 01:58:58PM

I always get "awk division by zero" on 10.6.
Is your awk-overkill for 10.6? I'd love to cut down on the number of commands and pipes of the original script...



[ Reply to This | # ]
See battery percentage in Terminal and GeekTool
Authored by: metiure on Feb 03, '10 02:50:57AM

I get that error too.
AMF, none of these scripts work with me (PPC, G4, 10.5.8)



[ Reply to This | # ]
See battery percentage in Terminal and GeekTool
Authored by: mael on Feb 05, '10 10:46:57AM

Although it doesn't work on 10.6.2 for me I'd love to understand it. Could anybody detail this awk-monster in plain english, a little?
Pretty please? :-)

Edited on Feb 05, '10 10:58:27AM by mael



[ Reply to This | # ]
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 | # ]
See battery percentage in Terminal and GeekTool
Authored by: lihtox on Feb 06, '10 06:43:48AM
Here's an easier to read form (you can paste it into the terminal even with the line breaks, although you can also remove them and stuff them into one line if you like. A multiple line command is no trouble for GeekTool.)

ioreg -l |awk 'BEGIN{FS="=";max=0;cur=0;}
$1~/CurrentCapacity/{cur=$2} 
$1~/MaxCapacity/{max=$2}
END{if (max>0) {printf "%.2f%%\n",cur/max*100} else {print "?"}}'
One modification I made is to define the "field separator" to "=" (FS="="), which tells awk that the columns it reads are separated by equals signs, rather than the default tab character. This should be less fragile in case the format of ioreg -l changes for some reason.

[ Reply to This | # ]
See battery percentage in Terminal and GeekTool
Authored by: mattswain123 on Feb 02, '10 03:46:21AM
Everyone should share their GeekTool scripts over at http://www.macosxtips.co.uk/geeklets/

[ Reply to This | # ]
See battery percentage in Terminal and GeekTool
Authored by: mattklunker on Feb 02, '10 07:44:33AM

Hallo,
how about : pmset -g ps

In Geektool i use :

pmset -g ps | sed -e '$!N;s/\n/ /' -e 's/\;\ AC\ attached\;\ not\ charging//;s/discharging\;//' -e 's/Currenty\ drawing\ from/\ /;s/\ \ -Internal/\ \ \|\ \ /;s/-0./\ :\ \ /;s/\;/\ \ \ \\ /g' | iconv -f utf-8 -t ucs-2-internal

Bye
Matt



[ Reply to This | # ]
See battery percentage in Terminal and GeekTool
Authored by: mael on Feb 05, '10 10:57:02AM
This works for me in 10.6 and 10.5:
pmset -g ps  |  sed -n 's/.*[[:blank:]]+*\(.*%\).*/\1/p'


[ Reply to This | # ]
See battery percentage in Terminal and GeekTool
Authored by: mael on Dec 15, '10 03:12:40AM

Unfortunately pmset spams my system.log with useless messages like
"executing /usr/bin/pmset"

The following command is quicker, more precise, does not make any entries in any log and works for me. It does look a little more complex, though:

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



[ Reply to This | # ]