|
|
See battery percentage in Terminal and GeekTool
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
See battery percentage in Terminal and GeekTool
I always get "awk division by zero" on 10.6.
See battery percentage in Terminal and GeekTool
I get that error too.
See battery percentage in Terminal and GeekTool
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? Edited on Feb 05, '10 10:58:27AM by mael
See battery percentage in Terminal and GeekTool
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 ("%%").
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
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.
See battery percentage in Terminal and GeekTool
I just realized I've never thanked you for explaining the commands. Edited on Dec 15, '10 03:09:14AM by mael
See battery percentage in Terminal and GeekTool
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.)
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.
|
SearchFrom our Sponsor...Latest Mountain Lion HintsWhat's New:HintsNo new hintsComments last 2 daysLinks last 2 weeksNo recent new linksWhat's New in the Forums?
Hints by TopicNews from Macworld
From Our Sponsors |
|
Copyright © 2014 IDG Consumer & SMB (Privacy Policy) Contact Us All trademarks and copyrights on this page are owned by their respective owners. |
Visit other IDG sites: |
|
|
|
Created this page in 0.06 seconds |
|