![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | 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 here. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How to convert a single column into several rows and columns? | ashton_smith | UNIX for Dummies Questions & Answers | 5 | 05-24-2008 01:44 PM |
| convert rows into column | cdfd123 | Shell Programming and Scripting | 3 | 01-11-2008 09:54 AM |
| generate rows from date. | GrepMe | Shell Programming and Scripting | 14 | 08-23-2007 10:46 PM |
| splitting a column into rows | spindoctor | UNIX for Dummies Questions & Answers | 3 | 07-24-2007 12:25 PM |
| Factorize some rows in a column | frebo | UNIX for Dummies Questions & Answers | 5 | 03-21-2006 03:41 AM |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Requesting an AWK code to generate averaged rows in a column
Hello,
I request your kind help in solving this problem... I have a file with two columns and "n" number of rows. For the first column, it won't be change. For the second column, I want to take the average of the first three rows. Then assign the averaged value to the first three rows. This process will be applied as well to the following 3 rows, after the changed rows. The output file will have two columns, the unchanged column ($1) and the changed column ($2). To illustrate the problem... Input file: $1= 1, 2, 3, 4, 5, 6.....n $2= 2, 4, 6, 8, 10, 12....n Output file: $1= no change $2= 4, 4, 4, 10, 10, 10....n (average of 2,4,6 is 4. average of 8,10,12 is 10) So far I have an AWK code that doesn't work: BEGIN {a[NR]=$2} { for (i=1;i<=n;i++) { avg=(a[i]+a[i+1]+a[i+2])/3; a[i]=avg; a[i+1]=avg; a[i+3]=avg; i=i+4; }; } END { i=1; while (i<=n) {print $1, a[i]; i+=1;}; } I'll appreciate a lot your help! Solracq |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
You show an example with 2 rows and n columns, do you want the average of 3 rows (= lines) or 3 columns?
It should be easier if you provide the exact format of the input and the desired output. |
|
#3
|
|||
|
|||
|
Quote:
awkscript.awk Code:
{if( NR == 2 )
{
i=1;
n=split($2,arr,",");
while(i<=n)
{
avg=(arr[i] + arr[i+1] + arr[i+2])/3;
arr[i]=arr[i+1]=arr[i+2]=avg;
i=i+3;
}
ORS="";
print $1"=";
ORS=","
for(i=1;i<=n;i++){
print arr[i];
}
};
}
|
|
#4
|
|||
|
|||
|
Using this code, I will try to make it work... thanks anyways!
|
|
#5
|
|||
|
|||
|
Clarification of my request (with my real input file)
Quote:
Input file (the one i'm using, the "......" means a space line): $1........$2 10......1348480 11......1364490 12......1347840 13......1414400 14.......1364480 15.......1331200 The output shuld be : $1........$2 10......1353603.3 11......1353603.3 12......1353603.3 ------------->Average of the first three lines of $2 13......1370026.6 14.......1370026.6 15.......1370026.6------------->Average of the following three lines of $2 the calculation ends till the final record according to the input (approx. 200 records) I think this makes more clear what I need... thanks again for you nice help! Solracq |
|
#6
|
|||
|
|||
|
Assuming you don't have the first row ($1........$2) in your file:
Code:
awk '
{s+=$2;a[NR%3]=$1}
!(NR%3){
for(i=1;i<4;i++) {
print a[i%3],s/3
}
s=0
}' file
|
|
#7
|
|||
|
|||
|
Thanks
IT WORKS!!
Thank you again! Carlos |
|||
| Google The UNIX and Linux Forums |