AWK: calculate ratio of columns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting AWK: calculate ratio of columns
# 1  
Old 02-09-2012
AWK: calculate ratio of columns

Hi all,


I have a tab-delimited text file in which i have a few columns which look like,
Code:
X     Y     U    V
2     3     4     5
4     5     3     4
6     4     3     2

For example, I want to calculate the ratio (X+Y)/(X+Y+U+V) for each row and print the output.
Code:
X     Y     U    V       ratio
2     3     4     5       0.357
4     5     3     4       0.562
6     4     3     2       0.666


Can anyone hep using awk.

Last edited by Franklin52; 02-10-2012 at 03:40 AM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 02-09-2012
Code:
awk 'NR==1{$5="ratio"}NR>1{$5=($1+$2)/($1+$2+$3+$4)}1' file

These 2 Users Gave Thanks to bartus11 For This Post:
# 3  
Old 02-09-2012
Quote:
Originally Posted by bartus11
Code:
awk 'NR==1{$5="ratio"}NR>1{$5=($1+$2)/($1+$2+$3+$4)}1' file



Hi,

It worked and i too tried in the same way but, didn't put 1 in the end. What does it stand for?
# 4  
Old 02-09-2012
It is same as:
Code:
awk 'NR==1{$5="ratio"}NR>1{$5=($1+$2)/($1+$2+$3+$4)}{print}' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Calculate percentage difference between two columns

I have a input text file in this format: ITEM1 10.9 20.1 ITEM2 11.6 12 ITEM3 14 15.7 ITEM5 20 50.6 ITEM6 25 23.6 I want to print those lines which have more than 5% difference between second and third columns. (8 Replies)
Discussion started by: ctrld
8 Replies

2. Shell Programming and Scripting

Match first two columns and calculate percent of average in third column

I have the need to match the first two columns and when they match, calculate the percent of average for the third columns. The following awk script does not give me the expected results. awk 'NR==FNR {T=$3; next} $1,$2 in T {P=T/$3*100; printf "%s %s %.0f\n", $1, $2, (P>=0)?P:-P}' diff.file... (1 Reply)
Discussion started by: ncwxpanther
1 Replies

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

4. Shell Programming and Scripting

Perl script to calculate failure ratio

Hello geeks, Find below a Perl script am writing to calculate some failure rate in our GPRS network, am just starting to use perl for scripting. #!/usr/bin/perl #Script written to calculate the following: #PDP activation failure rate for every 15 minutes interval #Number of Active PDP... (1 Reply)
Discussion started by: infinitydon
1 Replies

5. Shell Programming and Scripting

How to calculate average of two columns and copy into another file?

Hi, I need help with the awk command. I have a folder with aprox 500 files each one with two columns and I want to print in a new file, the average of column 1 and average of column 2 and the name of each file. Input files are: File-1: 100 99 20 99 50 99 50 99 File-2: 200 85... (3 Replies)
Discussion started by: Lokaps
3 Replies

6. Shell Programming and Scripting

Calculate the difference of two columns and keep the line with specific value

Hi everyone, I am trying to find sty all day. I have two files: File 1: N 82 AAA A 1 0.67 N 83 BBB B 1 0.79 N 84 CCC C 1 0.11 File 2: N 82 AAA A 1 0.63 N 83 BBB B 1 0.03 N 84 CCC C 1 0.08 I want to calculate... (2 Replies)
Discussion started by: Tzole
2 Replies

7. Shell Programming and Scripting

Computing the ratio of similar columns in the two files using awk script

Thanks Bartus11 for your help in the following code to compare the two files "t1" and "t2". awk 'NR==FNR{a=1;next}$2 in a{print $2}' t1 t2 First can anyone explain that what is the purpose of assigning a =1? Second, the current script is printing out the matched columns between the... (4 Replies)
Discussion started by: coder83
4 Replies

8. Shell Programming and Scripting

Calculate different between two columns and return high and lower value

In database.txt tourID:name:buyin:buyout 0001:SundayMillion:210:1000000 0002:MondayMillion:115:114 0003:Tuesday:123:0 0004:Thrusday:12412:26 I want the outpout: The higher value is SundayMillion, proffit 999790 euros. The lower value is MondayMillion, proffit 1 euros. The value more... (6 Replies)
Discussion started by: rafazz
6 Replies

9. Shell Programming and Scripting

How to calculate the difference between two adjacent columns?

Dear All, I need to find the difference between two adjacent columns. The file is having 'i' columns and i need to find the difference between two adjacent columns (like $1 difference $2; $2 difference $3; .... and $(i-1) difference $i). I have used the following coding awk '{ for (i=1; i<NF;... (7 Replies)
Discussion started by: Fredrick
7 Replies

10. Solaris

Fragmentation Ratio

All. How can i calculate the fragmentation ratio on a mounted disk, given that i have no root privilege and i cannot switch to single user mode. (0 Replies)
Discussion started by: Negm
0 Replies
Login or Register to Ask a Question