How to calculate the difference between two adjacent columns?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to calculate the difference between two adjacent columns?
# 1  
Old 12-03-2009
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

HTML Code:
awk '{ for (i=1; i<NF; i++) if (($i - $i+1) > 10) print 1; else print 0}' input > output
While using the above in the command line, i am getting the values only in the first column of the output file. I need to print the difference in columns.
(for example; output $1 = input $1 difference input $2...etc..)

The problem here is, i need to get the difference between the two adjacent columns (for example: if the first column first element is 10 and the second column first element is 40, then $1 - $2 = -30, which is less than 10, so the output will be 0 in this case) But if you see the difference is 30. So i need to check the difference is > 10 or not.

Is there any difference operator in awk? (like $1 ~ $2).
I need your help in this regard.

Expecting your reply and thanks in advance.

Regards
Fredrick.
# 2  
Old 12-03-2009
Replace $i+1 with $(i+1)
Code:
awk '{ for (i=1; i<NF; i++) if (($i - $i+1) > 10) print 1; else print 0}' input > output

Jean-Pierre.
# 3  
Old 12-03-2009
Code:
awk 'function abs(val) {return val>0?val:-val } 
for (i=1; i<NF; i++) {if (abs($i - $(i+1))>10) print 1; else print 0}' urfile


Last edited by rdcwayx; 12-03-2009 at 07:35 PM..
# 4  
Old 12-04-2009
Thank you "rdcwayx", but the output is having only one column, i need the output in the same was as the inputs are. Can you help me in this regard?

HTML Code:
awk 'function abs(val) {return val>0?val:-val } 
for (i=1; i<NF; i++) {if (abs($i - $(i+1))>10) print 1; else print 0}' urfile
Warm regards
Fredrick.
# 5  
Old 12-04-2009
Quote:
Originally Posted by Fredrick
Thank you "rdcwayx", but the output is having only one column, i need the output in the same was as the inputs are. Can you help me in this regard?

HTML Code:
awk 'function abs(val) {return val>0?val:-val } 
for (i=1; i<NF; i++) {if (abs($i - $(i+1))>10) print 1; else print 0}' urfile
Warm regards
Fredrick.
Code:
awk 'function abs(val) {return val>0?val:-val }
{for (i=1; i<NF; i++) {if (abs($i - $(i+1))>10) printf "1 "; else printf "0 "}} {print "\n"}' urfile

# 6  
Old 12-04-2009
Thank you very much for your prompt reply. I have tried with the following code, but i couldn't get the output as columns, it giving me output in a single column.

Code:
awk 'function abs(val) {return val>0?val:-val }
{for (i=1; i<NF; i++) {if (abs($i - $(i+1))>10) printf "1 "; else printf "0 "}} {print "\n"}' urfile

Can you suggest some other way to get it?
I need the output in the same format (new_column1=col1 diff col2 (0 or 1), new_column2=col2 diff col3 (0 or 1), etc....)

Thanks in advance

Fredrick.
# 7  
Old 12-06-2009
Quote:
Originally Posted by Fredrick
Thank you very much for your prompt reply. I have tried with the following code, but i couldn't get the output as columns, it giving me output in a single column.

Code:
awk 'function abs(val) {return val>0?val:-val }
{for (i=1; i<NF; i++) {if (abs($i - $(i+1))>10) printf "1 "; else printf "0 "}} {print "\n"}' urfile

Can you suggest some other way to get it?
I need the output in the same format (new_column1=col1 diff col2 (0 or 1), new_column2=col2 diff col3 (0 or 1), etc....)

Thanks in advance

Fredrick.
So why not give us the sample input and output directly.

What you need do by yourself is to adjust printf output. Here is the sample:

Code:
awk 'function abs(val) {return val>0?val:-val }
{for (i=1; i<NF; i++) {if (abs($i - $(i+1))>10) printf $i" diff " $(i+1)" (1), "; else printf $i" diff " $(i+1)" (0), "}} {print "\n"}' urfile

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 difference of split and sum the difference

In the awk I am trying to subtract the difference $3-$2 of each matching $4 before the first _ (underscore) and print that value in $13. I think the awk will do that, but added comments. What I am not sure off is how to add a line or lines that will add sum each matching $13 value and put it in... (2 Replies)
Discussion started by: cmccabe
2 Replies

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

3. Shell Programming and Scripting

How to calculate difference:?

Experts, file1 : Want to find the difference of $3 field from next line's 3rd field, The difference to be calculated from next lines 3rd field, to current lines lines 3rd field. file1 : Jun24_2013.06242013 3301244928 3133059904 167370640 95% Jun25_1124.06252013 3301244928... (4 Replies)
Discussion started by: rveri
4 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 the difference between two dates?

I want the difference between two following date using scripts in terms of no.of days. How I can accomplish this. lastdate=Tue Nov 13 10:30:56 2012 currdate=Wed Dec 15 15:58:21 PAKST 2012 Ouput should be like this: Your Password will expire after = 32 Days on Wed Dec 15 15:58:21 PAKST... (1 Reply)
Discussion started by: m_raheelahmed
1 Replies

6. UNIX for Dummies Questions & Answers

help to identify duplicate columns adjacent value

Hi friends, I have a xlsheet like below first column having id ABCfollowed by 7digit numbers and the next column have title against the ids. Titles are unique and duplicateboth, but ids are unique even for duplicate title.Now I need to identify those duplicate title having the highest id for... (9 Replies)
Discussion started by: umapearl
9 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 time difference.

Hi All, I've written a script which reads all the systems backup information and saves it in a log file. ssh -l ora${sid} ${primaryhost} "tail -1 /oracle/$ORACLE_SID/sapbackup/back$ORACLE_SID.log" | awk '{print $3,$4,$5,$6}' >> ${RESULTFILE} The output comes as below: 2008-09-30 06.00.01... (2 Replies)
Discussion started by: suri.tyson
2 Replies

9. Shell Programming and Scripting

How to calculate the time difference...

Hi All, I've written a script which reads all the systems backup information and saves it in a log file. ssh -l ora${sid} ${primaryhost} "tail -2 /oracle/$ORACLE_SID/sapbackup/back$ORACLE_SID.log" |head -1 | awk '{print echo "PREVIOUS:-- Start Date&Time: " $3,$4,echo "|| End Date&Time:... (1 Reply)
Discussion started by: suri.tyson
1 Replies

10. Shell Programming and Scripting

How to calculate this time difference

Hi, Please help me in calculating the time difference between below mentioned timestamps. a=07/17/2007 02:20:00 AM MST b=07/17/2007 02:07:46 AM MST Thanks (2 Replies)
Discussion started by: Prat007
2 Replies
Login or Register to Ask a Question