The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


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

More UNIX and Linux Forum Topics You Might Find Helpful
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

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 05-02-2008
Registered User
 

Join Date: Apr 2008
Posts: 8
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
Reply With Quote
Forum Sponsor
  #2  
Old 05-02-2008
Moderator
 

Join Date: Feb 2007
Posts: 2,314
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.
Reply With Quote
  #3  
Old 05-02-2008
Registered User
 

Join Date: Dec 2006
Posts: 131
Post

Quote:
Originally Posted by solracq View Post
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
Reply With Quote
  #4  
Old 05-02-2008
Registered User
 

Join Date: Apr 2008
Posts: 8
Unhappy it doesn't work...

Using this code, I will try to make it work... thanks anyways!
Reply With Quote
  #5  
Old 05-02-2008
Registered User
 

Join Date: Apr 2008
Posts: 8
Clarification of my request (with my real input file)

Quote:
Originally Posted by Franklin52 View Post
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
Reply With Quote
  #6  
Old 05-03-2008
Moderator
 

Join Date: Feb 2007
Posts: 2,314
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
Reply With Quote
  #7  
Old 05-03-2008
Registered User
 

Join Date: Apr 2008
Posts: 8
Thanks

IT WORKS!!

Thank you again!

Carlos
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 12:14 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0