[Solved] awk Column difference | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

[Solved] awk Column difference

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 01-25-2013
chrissycc chrissycc is offline
Registered User
 
Join Date: Feb 2010
Last Activity: 4 February 2014, 7:45 AM EST
Posts: 17
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 01-25-2013
Yoda's Avatar
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 23 July 2014, 12:06 PM EDT
Location: Galactic Empire
Posts: 3,354
Thanks: 230
Thanked 1,190 Times in 1,123 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 01-25-2013
tukuyomi tukuyomi is offline
Registered User
 
Join Date: Aug 2009
Last Activity: 19 July 2014, 2:22 PM EDT
Location: France
Posts: 318
Thanks: 18
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 01-25-2013
chrissycc chrissycc is offline
Registered User
 
Join Date: Feb 2010
Last Activity: 4 February 2014, 7:45 AM EST
Posts: 17
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 Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 12:06 PM.