Need an awk script to calculate the percentage of value field and replace


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Need an awk script to calculate the percentage of value field and replace
# 1  
Old 05-21-2013
Need an awk script to calculate the percentage of value field and replace

I have a input file called file.txt with the following content:
Code:
john|622.5674603562933|8|br:1;cn:3;fr:1;jp:1;us:2
andy|0.0|12|**:3;br:1;ca:2;de:2;dz:1;fr:2;nl:1

in fourth filed of input file, calulate percentage of each sub filed seperated by ; semicolon and replace value with percentage .
i need output in below format
Code:
john|622.5674603562933|8|br:12;cn:38;fr:12;jp:12;us:25
andy|0.0|12|**:25;br:8;ca:17;de:17;dz:8;fr:17;nl:8

I have to pass the pararmeters in to the awk script and calculate the percentage.

pls advice me

Last edited by Franklin52; 05-21-2013 at 07:28 AM.. Reason: Please use code tags
# 2  
Old 05-21-2013
whats the logic behind calculating the percentage? and could you please post the code you have tried till now?
# 3  
Old 05-21-2013
i had extracted data like below
Code:
3
1
2
2
1
2
1

Code:
awk 'NR==FNR{t+=$1;next}{printf("%.f %s\n", $1/t*100, $2)}' file file
25
8
17
17
8
17
8

---------- Post updated at 03:14 PM ---------- Previous update was at 03:13 PM ----------

Code:
**:3;br:1;ca:2;de:2;dz:1;fr:2;nl:1
awk 'NR==FNR{t+=$1;next}{printf("%.f %s\n", $1/t*100, $2)}' file file
25
8
17
17
8
17
8


Last edited by Scott; 05-21-2013 at 08:57 AM.. Reason: Code tags
# 4  
Old 05-21-2013
pass your actual file to this..

Code:
 
awk -F"|" '{gsub(":",";",$4);n=split($4,A,";");{v=$1"|"$2"|"$3;for(i=1;i<=n;i++){if(!i%2){v=v":"int((A[i]/$3)*100)";"}else{v=v""A[i]}}};print v}' filename

# 5  
Old 05-21-2013
still its not working
input :
Code:
john|622.5674603562933|8|br:1;cn:3;fr:1;jp:1;us:2
andy|0.0|12|**:3;br:1;ca:2;de:2;dz:1;fr:2;nl:1

Code:
awk -F"|" '{gsub(":",";",$4);n=split($4,A,";");{v=$1"|"$2"|"$3;for(i=1;i<=n;i++){if(!i%2){v=v":"int((A[i]/$3)*100)";"}else{v=v""A[i]}}};print v}' filename
john|622.5674603562933|8br1cn3fr1jp1us2
andy|0.0|12**3br1ca2de2dz1fr2nl1
||

but i need below output
Code:
john|622.5674603562933|8|br:12;cn:38;fr:12;jp:12;us:25
andy|0.0|12|**:25;br:8;ca:17;de:17;dz:8;fr:17;nl:8


Last edited by Scott; 05-21-2013 at 08:59 AM.. Reason: Code tags
# 6  
Old 05-21-2013
not sure what you missing its working fine on AIX

Code:
 
HOME>cat vv
john|622.5674603562933|8|br:1;cn:3;fr:1;jp:1;us:2
andy|0.0|12|**:3;br:1;ca:2;de:2;dz:1;fr:2;nl:1
HOME>awk -F"|" '{gsub(":",";",$4);n=split($4,A,";");{v=$1"|"$2"|"$3"|";for(i=1;i<=n;i++){if(!i%2){v=v":"int((A[i]/$3)*100)";"}else{v=v""A[i]}}};print v}' vv
john|622.5674603562933|8|br:12;cn:37;fr:12;jp:12;us:25;
andy|0.0|12|**:25;br:8;ca:16;de:16;dz:8;fr:16;nl:8;
HOME>

---------- Post updated at 04:12 PM ---------- Previous update was at 04:10 PM ----------

small change in command

Code:
 
awk -F"|" '{gsub(":",";",$4);n=split($4,A,";");{v=$1"|"$2"|"$3"|";for(i=1;i<=n;i++){if(!i%2){v=v":"int((A[i]/$3)*100+0.5)";"}else{v=v""A[i]}}};print v}' filename

# 7  
Old 05-21-2013
its not working Red Hat Enterprise Linux Server release 5.6 (Tikanga).
same error
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to calculate total and percent off field in file

Trying to use awk to print the lines in file that have either REF or SNV in $3, add a header line, sort by $4 in numerical order. The below code does that already, but where I am stuck is on the last part where the total lines are counted and printed under Total_Targets, under Targets_less_than is... (4 Replies)
Discussion started by: cmccabe
4 Replies

2. Shell Programming and Scripting

awk to output the percentage of a field compared to length

The awk below using the sample input would output the following: Basically, it averages the text in $5 that matches if $7 < 30 . awk '{if(len==0){last=$5;total=$7;len=1;getline}if($5!=last){printf("%s\t%f\n", last,... (6 Replies)
Discussion started by: cmccabe
6 Replies

3. Shell Programming and Scripting

Calculate Percentage

Hello, Ive got a bunch of numbers here e.g: 6065 6094 6348 6297 6161 6377 6338 6290 How do I find out if there is a difference between 10% or more between one of these numbers ? I am trying to do this in Bash.. but no luck so far.. Does anyone have an Idea ?? Thanx, - Pascal... (9 Replies)
Discussion started by: denbekker
9 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

Calculate percentage of a value accross m

I have 100 csv files like: file_city_1 file_city_2 file_city_3 file_city_4 City name is variable, there is 25 cities, each city has 4 region. Each of the 4 region contain some statistics like: parameter1 : number1 parameter1 : number2 ..... parameter50 : number50 ... (7 Replies)
Discussion started by: Meacham12
7 Replies

6. Shell Programming and Scripting

Need an awk script to calculate the percentage of value field and replace

Need an awk script to calculate the percentage of value field and replace I have a input file called file.txt with the following content: john|622.5674603562933|8|br:1;cn:3;fr:1;jp:1;us:2 andy|0.0|12|**:3;br:1;ca:2;de:2;dz:1;fr:2;nl:1 in fourth filed of input file, calulate percentage of each... (1 Reply)
Discussion started by: veeruasu
1 Replies

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

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

How can i calculate percentage ??

i have 3 files like total.dat=18 equal.dat=14 notequal.dat=16 i need find the equal percentange means: equalpercentage = ($equal.dat / $total.dat * 100) How i can do this ? I tried some of the answers to calculate the percentage in this forums.but it couldn't worked.Some one please... (6 Replies)
Discussion started by: bobprabhu
6 Replies

10. Programming

how do I calculate percentage ?

int percent (int a, int b) { if (b/a*100 > 25) return TRUE; else return FALSE; } I want to calculate what percentage of a is b. say if b = 48, a = 100 so b is 48% of a but wouldnt b/a give me 0 ??? what can be done ?? (6 Replies)
Discussion started by: the_learner
6 Replies
Login or Register to Ask a Question