Calculate percentage difference between two columns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Calculate percentage difference between two columns
# 1  
Old 07-23-2016
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 [negative or positive] between second and third columns.
# 2  
Old 07-23-2016
Hello ctrld,

Could you please try following and let me know if this helps you.
Code:
awk '{if((Q=100-(($2 * 100)/$3)) > 5 || Q <= -5){print Q, $0}}'  Input_file

Thanks,
R. Singh

Last edited by RavinderSingh13; 07-23-2016 at 03:00 PM..
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 07-23-2016
Appreciate your quick help. I got following output:
Code:
awk '{if((Q=100-(($2 * 100)/$3)) > 5 || Q >= -5){print Q, $0}}' input.txt
45.7711 ITEM1 10.9 20.1
3.33333 ITEM2 11.6 12
10.828 ITEM3 14 15.7
60.4743 ITEM5 20 50.6

Seems like a small tweak may be required to eliminate ITEM2 .
# 4  
Old 07-23-2016
Quote:
Originally Posted by ctrld
Appreciate your quick help. I got following output:
Code:
awk '{if((Q=100-(($2 * 100)/$3)) > 5 || Q >= -5){print Q, $0}}' input.txt
45.7711 ITEM1 10.9 20.1
3.33333 ITEM2 11.6 12
10.828 ITEM3 14 15.7
60.4743 ITEM5 20 50.6

Seems like a small tweak may be required to eliminate ITEM2 .
Hello ctrld,

As you had mentioned to take 5% in positve and negative both ways so I had kept it, could you please try following and let me know then if this helps you.
Code:
awk '{if((Q=100-(($2 * 100)/$3)) > 5){print Q, $0}}'  Input_file

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 5  
Old 07-23-2016
This one works perfect.

Code:
awk '{if((Q=100-(($2 * 100)/$3)) > 5){print Q, $0}}' input.txt
45.7711 ITEM1 10.9 20.1
10.828 ITEM3 14 15.7
60.4743 ITEM5 20 50.6

Seems like awk is not considering if its negative or positive difference.
# 6  
Old 07-23-2016
This one works perfect.

Code:
awk '{if((Q=100-(($2 * 100)/$3)) > 5){print Q, $0}}' input.txt
45.7711 ITEM1 10.9 20.1
10.828 ITEM3 14 15.7
60.4743 ITEM5 20 50.6

Seems like awk is not considering if its negative or positive difference.
Thanks a lot for your quick assistance in this query.
# 7  
Old 07-23-2016
Quote:
Originally Posted by ctrld
This one works perfect.

Code:
awk '{if((Q=100-(($2 * 100)/$3)) > 5){print Q, $0}}' input.txt
45.7711 ITEM1 10.9 20.1
10.828 ITEM3 14 15.7
60.4743 ITEM5 20 50.6

Seems like awk is not considering if its negative or positive difference.
Hello ctrld,

No, awk considers the difference between +5 and -5, offcourse if you will not put and +ve or -ve sign with any digit as per basic mathematics it will considered as a positive number, you could put -5 there and could see it will calculate from there.

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
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

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

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

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

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

5. Shell Programming and Scripting

How to calculate what percentage of X value is there in the file?

Input File: 5081 2058 175 8282 2358 7347 6612 3459 END OF INPUT FILE I need to know how to calculate minimum,maximum,average of the values in the file and also what percentage is the values over some user defined value for example 1000 and what percentage of value is over 5000. By... (2 Replies)
Discussion started by: aroragaurav.84
2 Replies

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

7. Shell Programming and Scripting

Calculate age of a file | calculate time difference

Hello, I'm trying to create a shell script (#!/bin/sh) which should tell me the age of a file in minutes... I have a process, which delivers me all 15 minutes a new file and I want to have a monitoring script, which sends me an email, if the present file is older than 20 minutes. To do... (10 Replies)
Discussion started by: worm
10 Replies

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

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