The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
grep unix.com with google



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

Reply
English Japanese Spanish French German Portuguese Italian Powered by Powered by Google
 
Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 12-01-2009
Registered User
 

Join Date: May 2009
Posts: 65
Averaging segments and including the name

Hello,
I have a awk line that averages rows.

So if my file looks like this:


Code:
Jack  1  1  1  1  1  1
Joe    1  1  1  1  1  1
Jerry  0  0  0  0  0  0
John  1  1  1  0  0  0

The awk line below skips column 1 and then averaged the rows


Code:
awk -F'\t' -v r=3 '{for(i=r+1;i<=NF;i+=r){for(j=0;j<r;j++){sum+=$(i-j)}printf "%s ", sum/r;sum=0}; printf "\n"}'

The end product looks like this

Code:
1  1  
1  1 
0  0
1  0

Basically what I want to do is print the first column as well... (the names)

So the end product will look like this:


Code:
Jack  1  1
Joe    1  1
Jerry  0  0
John  1  0

what modifications do I need to make?
thanks
  #2 (permalink)  
Old 12-01-2009
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,145
I don't fully understand the logic, but.... I think you want this:

Code:
awk -F'\t' -v r=3 '
  {
    for(i=r+1;i<=NF;i+=r){
       printf("%s%c", $1, OFS)
       for(j=0;j<r;j++)
          sum+=$(i-j)
       printf "%s ", sum/r;sum=0
   }
   printf "\n"
 }' myFile

  #3 (permalink)  
Old 12-01-2009
Registered User
 

Join Date: May 2009
Posts: 65
binning

basically I want to bin starting at column 2 (and also print the first column). The bin size can vary , but in this case its 3.

I had some problems with that line, I dont quite understand the change.
  #4 (permalink)  
Old 12-02-2009
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,145

Code:
awk -F'\t' -v r=3 '
{
  printf("%s%c", $1, OFS)
  for(i=2;i<=NF; i+=r) {
     for(j=0; j<r;j++)
       sum+=$(i+j)
     printf("%d%c", sum/r, (i+j-1==NF)?ORS:OFS)
     sum=0
  }
}' myFile

Sponsored Links
Reply

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 Off


More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Averaging Muhammad Rahiz UNIX for Dummies Questions & Answers 2 11-12-2009 06:00 AM
Averaging multiple columns gisele_l Shell Programming and Scripting 4 10-18-2009 06:53 PM
Averaging segments kylle345 Shell Programming and Scripting 11 09-16-2009 01:04 PM
averaging column values with awk johnmillsbro Shell Programming and Scripting 18 01-26-2009 01:20 PM
AWK - averaging $3 by info in $1 itisthus Shell Programming and Scripting 3 11-03-2008 06:12 AM



All times are GMT -4. The time now is 02:26 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