|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
[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
|
||||
|
||||
|
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
}' fileHere 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
|
|||
|
|||
|
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
|
|||
|
|||
|
Brilliant, both work a treat, thank you!
And I _think_ I follow what is going on, bonus! |
| Sponsored Links | ||
|
![]() |
| Thread Tools | Search this Thread |
| 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 |
|
|