Difference between two columns and count


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Difference between two columns and count
# 1  
Old 04-25-2011
Difference between two columns and count

hi I am very new to shell scripting. i wanted to achieve this..

 
Col1 Col2 Col3 Col4
aa 23 bb 32
aa 34 bb 12
aa 45 bb 345
kk 20 ss 50
kk 30 ss 50
tt 10 vv 50

Desired output is

 
Col1 Col2 Col3 Col4 Difference Count
aa 23 bb 30 7 2
aa 34 bb 14 -20 2
aa 45 bb 345 300 2
kk 20 ss 50 30 2
kk 30 ss 50 20 2
tt 10 vv 50 40 1

The difference column is [Col4 - col2] and Count column should only count number of rows if the value is between -50 and 50. I had one code but it is breaking in between and taking many hours to run..

Thanks for the help!!!

Last edited by empyrean; 04-25-2011 at 12:24 AM..
# 2  
Old 04-25-2011
I am trying and could do till adding difference column and i need to count them now. This is what i used to do

Quote:
awk '{a=$4;b=$2} {if(a-b<=50 && a-b>-50) print $0,a-b }' tablename
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

Compute Difference and Edit second, third columns

Hi Friends, My input file is like this chr1 100 200 chr1 300 330 chr1 2000 2000 chr1 5000 5000 chr2 7790 7890 chr2 8000 8000 If the difference of third and second columns is zero, then subtract 500 from second column and add 500 to the third column. So, my output would be chr1... (1 Reply)
Discussion started by: jacobs.smith
1 Replies

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

4. Shell Programming and Scripting

[Solved] sum up third and second columns by 0 difference

Hi Friends, I have the following file chr1 1 2 chr1 2 3 chr1 3 4 chr1 4 5 chr1 5 6 chr1 19 20 chr1 20 21 chr1 21 22 I want to compare the third column of record 1 to second column of next record and if the difference is zero, consider its third column and match it to next record... (4 Replies)
Discussion started by: jacobs.smith
4 Replies

5. Shell Programming and Scripting

Compare two columns in two files and print the difference

one file . . importing table employee 119 . . importing table jobs 1 2nd file . . importing table employee 120 . . importing table jobs 1 and would like... (2 Replies)
Discussion started by: jhonnyrip
2 Replies

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

7. Shell Programming and Scripting

Columns count

I'm writting a script that will get the logical volumes from a volume group and check for mirroring. I'm using a while read line command. I need to be able to count the columns for the output and if it is <= 3 save the logical volume to a file and if it is > 3 continue. How do I capture the... (1 Reply)
Discussion started by: daveisme
1 Replies

8. Shell Programming and Scripting

Extract difference of two columns from different rows

Hello guys, Please help me to solve this problem. I have tried some awk commands but couldn't succeed. I have a tab delimited file where each record is separated by ------ and 4th column of each record is same. <INPUT FILE> ------ peon 53931587 53931821 ... (12 Replies)
Discussion started by: sam_2921
12 Replies

9. Shell Programming and Scripting

Comparing Columns and printing the difference from a particular file

Gurus, I have one file which is having multiple columns and also this file is not always contain the exact columns; sometimes it contains 5 columns or 12 columns. Now, I need to find the difference from that particular file. Here is the sample file: param1 | 10 | 20 | 30 | param2 | 10 |... (6 Replies)
Discussion started by: buzzusa
6 Replies

10. UNIX for Dummies Questions & Answers

how to get difference of two columns

hey guys i want to divide the 2nd column with the first column using any command of unix help would be appreciated input set1 10 40 set2 12 60 output 2 20 (10 Replies)
Discussion started by: bogu0001
10 Replies
Login or Register to Ask a Question