Shell script to find Percentage?


 
Thread Tools Search this Thread
Top Forums Programming Shell script to find Percentage?
# 1  
Old 02-06-2012
Shell script to find Percentage?

Hi all,

I havea log of data.log
Code:
bear,10000,white
bear,5000,black
chicken,2000,white
chicken,4000,yellow
chicken,3000,black
lion,6000,yellow
lion,1000,white

How can we have shell script to get the percentage of each animals?

Thanks.
# 2  
Old 02-06-2012
awk would be better suited, really...

Code:
awk -F, '{ A[$1]+=$2; T+=$2 } END { for(X in A) printf("%s,%2d\n", X, (100*(A[X]/T))+0.5); }' filename

The 0.5 is to make sure the numbers round up/down properly, so the values total 100...
# 3  
Old 02-06-2012
Thanks, what if i have , then how you get percetage of each animal?

Code:
bear,10000,white
bear,3000,black
bear,2000,black
chicken,2000,white
chicken,4000,yellow
chicken,2000,yellow
chicken,3000,black
lion,6000,yellow
lion,1000,yellow
lion,1000,white

# 4  
Old 02-06-2012
In what way is the program I gave you not suitable? Did you try it?
# 5  
Old 02-06-2012
From
Code:
awk -F, '{ A[$1]+=$2; T+=$2 } END { for(X in A) printf("%s,%2d\n", X, (100*(A[X]/T))+0.5); }' filename

I got the output
Code:
lion,24
chicken,32
bear,44

I would like to have:
Code:
bear,white = (10000:(10000+3000+2000))X100%
bear,black = (3000+2000)(10000+3000+2000))X100%
....


Last edited by sabercats; 02-06-2012 at 06:21 PM.. Reason: add code
# 6  
Old 02-06-2012
So you want the colors and animal together, sure. But you don't actually want it to calculate it? Smilie

---------- Post updated at 04:30 PM ---------- Previous update was at 04:26 PM ----------

Code:
$ awk -F, '{ A[$1","$3]+=$2; T+=$2 } END { for(X in A) printf("%s=%2d\n", X, (100*(A[X]/T))+0.5); }' data
chicken,black=10
chicken,yellow=13
bear,black=16
lion,yellow=19
lion,white= 3
chicken,white= 6
bear,white=32

$

# 7  
Old 02-06-2012
Should it be
Code:
bear,10000,white
bear,3000,black
bear,2000,black
chicken,2000,white
chicken,4000,yellow
chicken,2000,yellow
chicken,3000,black
lion,6000,yellow
lion,1000,yellow
lion,1000,white

The out put will be
Code:
bear,white = 64%
bear,black= 33%
chicken,white=18%
chicken,yellow= 55%
chicken,black= 27%
lion,yellow= 87%
lion,1000,white=13%

I got the output
Code:
chicken,white, 6
chicken,black, 9
chicken,yellow,18
lion,white, 3
bear,white,29
lion,yellow,21
bear,black,15

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. UNIX for Beginners Questions & Answers

Working out percentage in shell script

Hi All, I currently have a shell script which is pulling multiple counters from various sources. Due to the counters being cumulative counters I've got some code to work out the delta from the last reading and current which is working fine. The problem i have now is being able to work out the... (8 Replies)
Discussion started by: mutley2202
8 Replies

3. UNIX for Dummies Questions & Answers

Percentage / Multiplication in Shell Script

Hello, I am trying to compute the percentage in a script as shown below: PerCover=`echo "scale=2 ; 100 \* ($InputCover/$Total)" | bc` However the PerCover value is blank/null. What do I need to do differently? Thanks for your input! ~Guss (1 Reply)
Discussion started by: Gussifinknottle
1 Replies

4. 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

5. Shell Programming and Scripting

Script shell, how to calculate percentage?

hello, please can you help me. jj and kk are two numbers which are the result of an sql program. I would like to calculate the ratio jj/kk*100. I have done this: ratio=$((jj/kk * 100)) or ratio=`expr $jj \/ expr $kk) but the result is 0 What can i do? Thanks for help. (3 Replies)
Discussion started by: likeaix
3 Replies

6. Shell Programming and Scripting

Help with bash script - Need to get CPU usage as a percentage

I'm writing a bash script to log some selections from a sensors output (core temp, mb temp, etc.) and I would also like to have the current cpu usage as a percentage. I have no idea how to go about getting it in a form that a bash script can use. For example, I would simply look in the output of... (3 Replies)
Discussion started by: graysky
3 Replies

7. Shell Programming and Scripting

awk script to count percentage from log file

Hi, I have a log like this : actually i want to get the log like this : where % can get from : 100 * pmTotNoRrcConnectReqSucc / pmTotNoRrcConnectReq Thanks in advance.. :) (8 Replies)
Discussion started by: justbow
8 Replies

8. Shell Programming and Scripting

Need to find the percentage of the directory in the file system.

Hi All, I want to find the percentage occupied by the directory in the file system. Say, i have the file system /home/arun/work under this file system i have the directories /home/arun/work/yesterday /home/arun/work/today /home/arun/work/tomorrow The size of the file system is... (5 Replies)
Discussion started by: Arunprasad
5 Replies

9. 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

10. Shell Programming and Scripting

how to make percentage of a running script?

dear friends, does anyone know how to make the percentage of proccess of a running script? :confused: like we always see in windows, when we install a program, there is always an indicator using percent value (from 0% to 100%) i need this for my time consuming script thanks before (1 Reply)
Discussion started by: jimmbp
1 Replies
Login or Register to Ask a Question