how to get difference of two columns


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers how to get difference of two columns
# 1  
Old 02-09-2009
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
# 2  
Old 02-09-2009
With a little help from the "Search" button:

https://www.unix.com/cfmgoogle.php?cx...sa=Search#1313
# 3  
Old 02-09-2009
Addition of the values. Sorry not substraction this time

Thanq u
but my file is little bit complicated. all values of column 1 and 2 are in 2 seperate single cells!!!!!!

input
column1
0,9607,12887,24088,
column2
579,248,404,385

output
579,9855,13291,24473
# 4  
Old 02-09-2009
sorry not subtraction , this time addition!!!!

!!!!!!!!!!!!!!!!!!!!!!!
# 5  
Old 02-09-2009
You can still use the little "Search" button, man. I found some threads similar with this one, but here goes:

Code:
awk 'BEGIN {FS=","} { for (i=1;i<=NF;i++) sum[i]=sum[i] + $i } END { for (i=1;i<=NF;i++) printf("%d,", sum[i]) }' foo

# 6  
Old 02-09-2009
but its printing the input numbers as it is in output
I'm unable to get the result
Sorry for the trouble man
# 7  
Old 02-09-2009
Hmmm...Could you post sample input and output?

I got the ff:

Code:
$ cat foo
0,9607,12887,24088,
579,248,404,385

$ awk 'BEGIN {FS=","} { for (i=1;i<=NF;i++) sum[i]=sum[i] + $i } END { for (i=1;i<=NF;i++) printf("%d,", sum[i]) }' foo
579,9855,13291,24473,

 
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

Finding difference between two columns of unequal length

Hi, I have two files which look like this cat waitstate.txt 18.2 82.1 cat gostate.txt 5.6 5.8 6.1 6.3 6.6 6.9 7.2 7.5 (4 Replies)
Discussion started by: jamie_123
4 Replies

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

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

[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

6. Shell Programming and Scripting

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 ... (1 Reply)
Discussion started by: empyrean
1 Replies

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

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

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

10. 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
Login or Register to Ask a Question