|
|
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
|
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.08 seconds |
|