Requesting an AWK code to generate averaged rows in a column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Requesting an AWK code to generate averaged rows in a column
# 1  
Old 05-02-2008
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
# 2  
Old 05-02-2008
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  
Old 05-02-2008
Java

Quote:
Originally Posted by solracq
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
Here is the script.Try this out
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];
    }
};
}

Invoke it as awk -F"=" -f awkscript.awk Inputfile.txt
# 4  
Old 05-02-2008
Data it doesn't work...

Using this code, I will try to make it work... thanks anyways!
# 5  
Old 05-02-2008
Clarification of my request (with my real input file)

Quote:
Originally Posted by Franklin52
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.
hi, I require the average of every three rows or lines in a column, $2. And I need to replace the result with the same three values, rows, I calulated the average. In other words...

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  
Old 05-03-2008
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

Regards
# 7  
Old 05-03-2008
Thanks

IT WORKS!!

Thank you again!

Carlos
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Compare values in multiple rows in one column using awk

I would like to compare values in column 8, and grep the ones where the different is > 1, columns 1 and 2 are the key for array. Every 4 rows the records values in columns 1 and 2 changed. Then, the comparison in the column 8 need to be done for the 4 rows everytime columns 1 and 2 changed ... (4 Replies)
Discussion started by: jiam912
4 Replies

2. UNIX for Advanced & Expert Users

Conversion of rows to columns using awk based om column value

HI, My Input file data is dn:adcfgeneral id:13343 Name:xxxxxx Password:iutyerwuitywue wpuwt tuiytruityrutyrwtyrwp dn:cdferwjyyyy id:3875 Name:yyyy Password :hgfdsjkfhdsfkdlshf dshfkldshfdklsfh interset:uiuiufj My output should be ... (6 Replies)
Discussion started by: dineshaila
6 Replies

3. Shell Programming and Scripting

Create new rows for each column value with awk

Hi, I have the following type of data that is separated by tabs: -2 abandonar abandono abandonas abandona abandonamos abandonáis abandonan -4 abandonado abandonada abandonados abandonadas -2 abandona abandonos ... (1 Reply)
Discussion started by: owwow14
1 Replies

4. Shell Programming and Scripting

Awk: Interchange the Rows and column

Hi, I have following input and want to change it to following output INPUT 01-APR-14,KB,822714 01-APR-14,MB,8133431 02-APR-14,KB,757140 02-APR-14,MB,7770368 03-APR-14,KB,815427 03-APR-14,MB,7590511 04-APR-14,MB,7529895 04-APR-14,KB,779561 05-APR-14,MB,8151537 05-APR-14,KB,809675 ... (6 Replies)
Discussion started by: siramitsharma
6 Replies

5. Shell Programming and Scripting

awk code to ignore the first occurence unknown number of rows in a data column

Hello experts, Shown below is the 2 column sample data(there are many data columns in actual input file), Key, Data A, 1 A, 2 A, 2 A, 3 A, 1 A, 1 A, 1 I need the below output. Key, Data A, 2 A, 2 A, 3 A, 1 A, 1 A, 1 (2 Replies)
Discussion started by: ks_reddy
2 Replies

6. Shell Programming and Scripting

awk to transpose preceding row to 1st column of next rows

Gurus: How can I transpose the output below to a format in which I can plot a graph to show VSZ memory usage by PIDs? stdout: Tue Jan 22 07:29:19 CUT 2013 42336296 1841272 java wilyadm 21889232 438616 jlaunch sidadm 42532994 414336 jlaunch sidadm Tue Jan 22 07:49:20 CUT 2013... (1 Reply)
Discussion started by: ux4me
1 Replies

7. Shell Programming and Scripting

awk command to print only selected rows in a particular column specified by column name

Dear All, I have a data file input.csv like below. (Only five column shown here for example.) Data1,StepNo,Data2,Data3,Data4 2,1,3,4,5 3,1,5,6,7 3,2,4,5,6 5,3,5,5,6 From this I want the below output Data1,StepNo,Data2,Data3,Data4 2,1,3,4,5 3,1,5,6,7 where the second column... (4 Replies)
Discussion started by: ks_reddy
4 Replies

8. Shell Programming and Scripting

awk transpose rows to column

Need to transpose in awk rows to column like this: input: A1,6,5,4 3,2,1, A2,8,7,9,10,11,12,13,14 A3,1,2,3,5,7,8,9 A4,9,4,8,1,5,3, output: A1,1 A1,2 A1,4 ... A2,7 A2,8 ... A3,1 A3,2 ... A4,1 A4,3 (5 Replies)
Discussion started by: sdf
5 Replies

9. UNIX for Dummies Questions & Answers

how to count number of rows and sum of column using awk

Hi All, I have the following input which i want to process using AWK. Rows,NC,amount 1,1202,0.192387 2,1201,0.111111 3,1201,0.123456 i want the following output count of rows = 3 ,sum of amount = 0.426954 Many thanks (2 Replies)
Discussion started by: pistachio
2 Replies

10. Shell Programming and Scripting

awk to select rows based on condition on column

I have got a file like this 003ABC00281020091005000100042.810001 ... (8 Replies)
Discussion started by: Maruti
8 Replies
Login or Register to Ask a Question