awk or sed to determine battery percentage


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk or sed to determine battery percentage
# 1  
Old 02-01-2010
awk or sed to determine battery percentage

So, I know this can be done but my awk and sed wizardry is not up to snuff for the job. Basically, I have tons and tons of Macbooks at my work and I am trying to grab the current percentage of the battery power on each client.

If I use the system_profiler SPPowerDataType command it prints off the current specs of all power management. One spec is the current charge of the battery in mAh, and one is the total capacity.

So, I would like to grep | awk | or sed out this info and then have it do simple division then convert the answer into readable percentage.

So like if I pull out the string of the current charge and it is at 1020 mAh, and I know that the total capacity of the battery is 5242 mAh. I could grab both of those strings but how to I make those form a number in percentage that is human readable?

Thanks a ton for any help in advance.

EDIT

Guess I should give some example code...

Code:
system_profiler SPPowerDataType | grep "Charging status" | awk '{ print 4}'

This returns the numerical number of how much charge, and I would need to grab the total capacity and the divide them or whatever to output a percentage.

Last edited by tlarkin; 02-01-2010 at 03:38 PM..
# 2  
Old 02-01-2010
Sample code to give you a percentage (assumes you have obtained the values into the CURRENT_CHARGE and BATTERY_CAPACITY variables, e.g.:
Code:
CURRENT_CHARGE=`system_profiler SPPowerDataType | grep "Charging status" | awk '{ print 4}'`

Sample code:
Code:
CURRENT_CHARGE=1020
BATTERY_CAPACITY=5242

PERCENT_CHARGED=`echo "scale=3; ${CURRENT_CHARGE} / ${BATTERY_CAPACITY} * 100" | bc`
echo "PERCENT_CHARGED = ${PERCENT_CHARGED}%"

Running a script containing the above code produces the output:
Code:
$ ./percent_calc_script.sh
PERCENT_CHARGED = 19.400%

# 3  
Old 02-02-2010
The only thing I am wondering is if every battery has the same maximum capacity. I also get a syntax error when trying your method. I know that I could do A / B *100 to get the percentage but I was hoping to do it in a one liner with awk or sed....

Thanks again for your input though it is definitely helpful.

Last edited by tlarkin; 02-02-2010 at 10:51 AM..
# 4  
Old 02-02-2010
Code:
# eval $(system_profiler SPPowerDataType | awk -F= '/CURRENT_CHARGE/{x=$NF}/BATTERY_CAPACITY/{printf "PERCENT_CHARGED=%.2f%c",(x/($NF/100)),"%"}')
# echo $PERCENT_CHARGED
19.46%

# 5  
Old 02-02-2010
Quote:
Originally Posted by danmero
Code:
# eval $(system_profiler SPPowerDataType | awk -F= '/CURRENT_CHARGE/{x=$NF}/BATTERY_CAPACITY/{printf "PERCENT_CHARGED=%.2f%c",(x/($NF/100)),"%"}')
# echo $PERCENT_CHARGED
19.46%

I can't get this to actually work, I am on 10.5.7...

It returns a blank line for the command. I tried it with just the command but have not put it in a script.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. HP-UX

Shell /awk script for Percentage

having two columns, A and B.. i need to add another column C in a file and calculate the percentage based on the column A and B. (COLUMN B/ COLUMN A *100) . "|" is delimiter separating the A and B.. need C column with the percentage value. Thanks for your help 100|50 |50% ... (6 Replies)
Discussion started by: kartikirans
6 Replies

2. Shell Programming and Scripting

Help with awk percentage calculation from a file

i have a file say test with the below mentioned details Folder Name Total space Space used /test/test1 500.1GB 112.0 GB /test/test2 3.2 TB 5TB /test/test3 3TB 100GB i need to calculate percentage of each row based on total space and space used and copy... (9 Replies)
Discussion started by: venkitesh
9 Replies

3. Shell Programming and Scripting

Percentage sign causing awk problems

looks like awk gets confused when there's a % next to a number. command im running: awk -F" " '/phxnaz001b/ && /vol/ && NF { if (($NF >= 80) && ($NF < 83)) { print ; print ; w++ } else if ($NF >= 83) { print ; c++ } } END { printf("%d:OK %d:WARNING %d:CRITICAL\n", o, w, c) }' /tmp/test.log ... (3 Replies)
Discussion started by: SkySmart
3 Replies

4. Shell Programming and Scripting

Calculate percentage of columns greater than certain value in a matrix using awk

This matrix represents correlation values. Is it possible to calculate the percentage of columns (a1, a2, a3) that have a value >= |0.5| and report the percentage that has positive correlation >0.5 and negative correlation <-0.5 separately. thanx in advance! input name a1 a2 a3... (5 Replies)
Discussion started by: quincyjones
5 Replies

5. Shell Programming and Scripting

find percentage - awk

Please help me with this ... Input file /vol/test1 10G /vol/test2 1G /vol/test3 200G /vol/test4 3G Output File /vol/test1 10G - - 9G - /vol/test2 1024M - - 921M - /vol/test3 200G - - 180G - /vol/test4 3072M - - 2764M - Basically if Column 2 ( which is... (6 Replies)
Discussion started by: greycells
6 Replies

6. UNIX for Dummies Questions & Answers

Using awk/sed to determine whether there are any 2's, 3's and 4's in a column

Hi, I have a very large file which is of the general format: 0 3 4 2 ... 3 2 4 0 ... 0 3 4 2 ... 3 0 4 2 ... . . . . ... . . . . ... I would like to apply a simple awk/sed script to work out whether there are any 2's, 3's and 4's in each column. The results would look as... (3 Replies)
Discussion started by: kasan0
3 Replies

7. Shell Programming and Scripting

awk/sed percentage calculation

Hi all i have a text file with columns delimited with , 2010-08-18,10,24,.09751,39,7,14872,26732 . . . i would to add a extra column in the end with percentage calculation of columns 5 and 8 ie (39/26732)*100 so the output must look like ... (6 Replies)
Discussion started by: posner
6 Replies

8. Shell Programming and Scripting

Need an AWK script to calculate the percentage

Hi I need a awk script to calculate percentage. I have to pass the pararmeters in to the awk script and calculate the percentage. Sum = 50 passed = 43 failed = 7 I need to pass these value in to the awk script and calculate the percentage. Please advice me. (8 Replies)
Discussion started by: bobprabhu
8 Replies

9. Shell Programming and Scripting

awk percentage

how would you calculate percentage by per line? Given a column of 16 lines, grab each line and divide it by the sum of the entire column and multiply by 100? thanks ... (8 Replies)
Discussion started by: rockiefx
8 Replies

10. HP-UX

How to determine cpu&memory percentage usage per user

Using HP-UX v11 Need to monitor cpu and memory usage, total for system and separately for each user in command-line mode. Found out next ways to monitor total cpu usage under hp-ux: 1) vmstat, also shows free memory 2) sar -M ps -eo user,pcpu - does not work, means 'user-defined format'... (4 Replies)
Discussion started by: hp-ux-user
4 Replies
Login or Register to Ask a Question