The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 and shell scripting languages 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 04:44 PM
convert rows into column cdfd123 Shell Programming and Scripting 3 01-11-2008 12:54 PM
generate rows from date. GrepMe Shell Programming and Scripting 14 08-24-2007 01:46 AM
splitting a column into rows spindoctor UNIX for Dummies Questions & Answers 3 07-24-2007 03:25 PM
Factorize some rows in a column frebo UNIX for Dummies Questions & Answers 5 03-21-2006 06:41 AM

 
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Prev Previous Post   Next Post Next
  #1 (permalink)  
Old 05-02-2008
solracq solracq is offline
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
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 01:21 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0