Grouping and calculation of percentages


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grouping and calculation of percentages
# 1  
Old 03-16-2012
Grouping and calculation of percentages

Hi,

I have a table like this,

Group type L1 L2 L3 L4 L5 L6
A xx1 0 3 3 2 1 0
A xx2 2 2 2 1 7 2
B yy1 2 4 6 6 3 1
C yy2 7 7 7 0 2 3
C zz2 8 8 2 3 3 1
For each column L1 to L6, I need to calculate percentage for each "type" per column total in each "Group".

So, desired output is
Group type L1 L2 L3 L4 L5 L6
A xx1 0*100/2 3*100/5 3*100/5 2*100/3 1*100/8 0*100/2
A xx2 2*100/2 2*100/5 2*100/5 1*100/3 7*100/8 2*100/2
B yy1 2*100/2 4*100/4 6*100/6 6*100/6 3*100/3 1*100/1
C yy2 7*100/15 7*100/15 7*100/9 0*100/3 2*100/5 3*100/4
C zz2 8*100/15 8*100/15 2*100/9 3*100/3 3*100/5 1*100/4
I typed formulas, not actual percentages just to make things clear.

thanks in advance. Smilie

Last edited by polsum; 03-17-2012 at 01:07 PM..
# 2  
Old 03-16-2012
Sorry, but none of your percentage calculations make sense.
For example:

2/5*100

Do you mean:

(2x100)/5 = 40% ?


Please post what Operating System and version and what Shell you are using.

Last edited by methyl; 03-16-2012 at 11:54 PM..
# 3  
Old 03-17-2012
Quote:
Originally Posted by methyl
Do you mean:
(2x100)/5 = 40% ?
Yes. I am using Ubuntu latest version. thanks

---------- Post updated 03-17-12 at 11:08 AM ---------- Previous update was 03-16-12 at 11:18 PM ----------

Anyone? I edited the desired output in the first post to make it more clear. thanks
# 4  
Old 03-17-2012
Try:
Code:
awk 'NR==FNR{for (i=3;i<=NF;i++) a[$1"-"i]+=$i;next}FNR>1{for (i=3;i<=NF;i++) $i=sprintf ("%.2f",($i*100)/a[$1"-"i])}1' file file

Notice that you have to specify filename twice.
This User Gave Thanks to bartus11 For This Post:
# 5  
Old 03-17-2012
Quote:
Originally Posted by bartus11
Try:
Code:
awk 'NR==FNR{for (i=3;i<=NF;i++) a[$1"-"i]+=$i;next}FNR>1{for (i=3;i<=NF;i++) $i=sprintf ("%.2f",($i*100)/a[$1"-"i])}1' file file

Notice that you have to specify filename twice.
You rock, as always !

I need an advise from you. I want to learn AWK, can you please suggest a book? or online tutorial? Thanks Smilie
# 6  
Old 03-17-2012
This User Gave Thanks to bartus11 For This Post:
# 7  
Old 03-17-2012
Quote:
Originally Posted by bartus11
Thanks.Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grouping and Calculating

Hi All, I want to read the input file and store the output in the Output file. I pasted the sample Input and Output file below. Help me with this. Input file ================================= ITEM1 AAAAA 1 ITEM1 BBBBB 1 ITEM1 CCCCC 1 ITEM2 AAAAA 5 ITEM2 CCCCC 4... (1 Reply)
Discussion started by: humaemo
1 Replies

2. Shell Programming and Scripting

Name grouping

awk 'FNR==NR {a; next} $NF in a' genes.txt refseq_exons.txt > output.txt I can not figure out how to group the same name in $4 together. Basically, all the SKI together in separate rows and all the TGFB2. Thank you :). chr1 2160133 2161174 SKI chr1 218518675 218520389 TGFB2... (1 Reply)
Discussion started by: cmccabe
1 Replies

3. Shell Programming and Scripting

Grouping

Hi all, I am using following command: perl program.pl input.txt output.txt CUTOFF 3 > groups_3.txt containing program.pl, two files (input.txt, output.txt) and getting output in groups_3.txt: But, I wish to have 30 files corresponding to each CUTOFF ranging from 0 to 30 using the same... (1 Reply)
Discussion started by: bioinfo
1 Replies

4. Shell Programming and Scripting

Diffing words - percentages

is there a way to do the following: say i have two words: WelcomeMattTom and WelcomeMTom How can i compare the two words to know how much alike, in percentages they are? like, how similar is WelcomeMTom to WelcomeMattTom? not clear yet? say i introduced a third word,... (6 Replies)
Discussion started by: SkySmart
6 Replies

5. Shell Programming and Scripting

Script to provide percentages?

so i'm have been stifled here inn my attempts at this. i need to calculate an unusual figure. what is the percentage difference between 400 and 3? usually, to get the percentage, you just divide the smaller number by the bigger number. then multiply the answer by 100. in this case... (10 Replies)
Discussion started by: SkySmart
10 Replies

6. Shell Programming and Scripting

Comparing sizes in percentages of 2 files in bash

Hi guys, I hope you can enlight me with a script I'm doing for Solaris 10. Script goes like this: #!/usr/bin/bash fechahoy=`perl /export/home/info/John/fechamod.pl` fechayer=`perl /export/home/info/John/fecha.pl` echo $fechahoy echo $fechayer DAT1=`ssh ivt@blahblah ls -la... (1 Reply)
Discussion started by: sr00t
1 Replies

7. Shell Programming and Scripting

Grouping each 2 lines

Whats a good way to group (by adding a new integer to the front of each line) pairs of lines, such that lines 1 & 2 are group 1, lines 3 & 4 are group 2, etc... ex input: A B C D etc... ex output: 1A 1B 2C 2D etc... (5 Replies)
Discussion started by: watingo
5 Replies

8. UNIX for Advanced & Expert Users

Regarding the user grouping

I am new to the unix server. My question is: i have 10 users a b c d e f g h i j Among the 10 5 users are (normal) user and remainging 5 users are (qad) users How can i sepearte this one? How can i give the dlc access rights? Please provide the clear cut idea. (0 Replies)
Discussion started by: kingganesh04
0 Replies

9. Shell Programming and Scripting

ksh script using expr to calculate percentages

Within a ksh script on HP-UX I trying to calculate a percentage of a number (number/100 x percentage) using the below method and expr. TARPERC=`expr 16 / 100 \* 5` TARSUM=`expr 16 + $TARPERC` ZIPSUM=`expr $TARSUM \* 2` If the input is 16 outputs are: TARPERC: 0 TARSUM: 16 ZIPSUM: 32... (6 Replies)
Discussion started by: wurzul
6 Replies

10. Shell Programming and Scripting

egrep and grouping

i am using the c shell on solaris. directories i'm working with: ls -1d DIV* DIV_dental/ DIV_ibc/ DIV_ifc/ DIV_index/ DIV_pharm/ DIV_sectionI/ DIV_sectionI-title/ DIV_sectionI-toc/ DIV_sectionII-title/ DIV_sectionII-toc/ DIV_standing/ DIV_standing-toc/ DIV_title/ DIV_vision/ (1 Reply)
Discussion started by: effigy
1 Replies
Login or Register to Ask a Question