Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

[Solved] awk Column difference

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 01-25-2013
chrissycc chrissycc is offline
Registered User
 
Join Date: Feb 2010
Last Activity: 12 September 2014, 4:40 PM EDT
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
[Solved] awk Column difference

Hi,

I've got what is probably quite an easy little (presumably) awk problem that I just can't seem to work out (mental block...I've already spent ages getting the data into this format!). I want to work out the difference between rows for certain columns. for example:


Code:
1359142876 RED 14 BLUE 3 GREEN 71 BLACK 65 
1359135610 RED 13 BLUE 3 GREEN 71 BLACK 65
1359128178 RED 10 BLUE 3 GREEN 68 BLACK 60 
1359121257 RED 8 BLUE 3 GREEN 66 BLACK 52 
ETC

to


Code:
1359142876 RED 14 BLUE 3 GREEN 71 BLACK 65 
1359135610 RED 1 BLUE 0 GREEN 0 BLACK 0
1359128178 RED 3 BLUE 0 GREEN 3 BLACK 5 
1359121257 RED 2 BLUE 0 GREEN 2 BLACK 8 
ETC

(contents of the first column doesn't matter).

The other thing is, we don't know how many columns there will be (could be additional pairs, "PINK 10" for example).

As always any help would be appreciated

Last edited by radoulov; 01-26-2013 at 03:40 AM.. Reason: Marked as solved.
Sponsored Links
    #2  
Old Unix and Linux 01-25-2013
Yoda's Unix or Linux Image
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 6 July 2015, 7:43 PM EDT
Location: Galactic Empire
Posts: 3,409
Thanks: 240
Thanked 1,217 Times in 1,143 Posts
Here is for fixed number of columns:

Code:
awk 'NR==1 {
 f3=$3;f5=$5;f7=$7;f9=$9;
 print
}NR!=1 {
 t3=$3;t5=$5;t7=$7;t9=$9;
 $3=f3-$3;$5=f5-$5;$7=f7-$7;$9=f9-$9;
 f3=t3;f5=t5;f7=t7;f9=t9;
 print
}' file

Here is generic code:

Code:
awk 'NR==1 {
 for(i=3;i<=NF;i+=2) {
  a[i]=$i;
 }
 print
}NR!=1 {
 for(j=3;j<=NF;j+=2) {
  t[j]=$j;
  $j=a[j]-$j;
  a[j]=t[j];
 }
 print
}' file

Sponsored Links
    #3  
Old Unix and Linux 01-25-2013
tukuyomi tukuyomi is offline
Registered User
 
Join Date: Aug 2009
Last Activity: 7 July 2015, 3:30 PM EDT
Location: France
Posts: 320
Thanks: 19
Thanked 81 Times in 76 Posts

Code:
awk '{for(f=3; f<=NF;f+=2){
        if(NR==1)A[f]=$f;else{B[f]=A[f]-$f;A[f]=$f; $f=B[f]}
}}1' file

    #4  
Old Unix and Linux 01-25-2013
chrissycc chrissycc is offline
Registered User
 
Join Date: Feb 2010
Last Activity: 12 September 2014, 4:40 PM EDT
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Brilliant, both work a treat, thank you!

And I _think_ I follow what is going on, bonus!
Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Script to find difference between 2 files by column shakthi666 Homework & Coursework Questions 1 01-09-2013 07:12 AM
Script to find difference between 2 files by column shakthi666 Shell Programming and Scripting 1 01-09-2013 02:03 AM
Calculate difference in timestamps based on unique column value asnandhakumar Shell Programming and Scripting 4 10-23-2012 10:02 AM
find difference in file column... malcomex999 Shell Programming and Scripting 1 04-15-2009 05:31 AM
script to compare first column of two files and find difference adityam Shell Programming and Scripting 3 10-22-2008 09:02 AM



All times are GMT -4. The time now is 05:56 PM.