The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM



Thread: average value
View Single Post in UNIX Forums - Click on the Thread or Permalink to View Entire Thread -->
  #3 (permalink)  
Old 03-10-2007
vgersh99's Avatar
vgersh99 vgersh99 is offline
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,165
nawk -f grid.awk myMatrixFile

grid.awk:
Code:
function outputGrid(i) {
  printf("%s\n", thisGrid)
  for(i=1; i<= col; i++)
     printf("%.1f%s", arr[i]/row, (i==col) ? RS : OFS)

  thisGrid=$0
  split("", arr)
  row=0
}

/^Grid/ {
  ( thisGrid != "") ? outputGrid() : thisGrid=$0
  next
}

/^[0-9][0-9]*/ {
  row++
  col=NF
  for(i=1; i <= NF; i++) {
    arr[i] += $i
  }
}
END {
  outputGrid()
}

Last edited by vgersh99; 03-10-2007 at 10:52 AM..
Reply With Quote