The UNIX and Linux Forums  


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




View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #6 (permalink)  
Old 08-30-2007
baghera baghera is offline
Registered User
  
 

Join Date: Aug 2007
Posts: 23
Quote:
Originally Posted by vgersh99 View Post
baghera,
I believe we've already gone through this exercise once in one of the previous threads. What is it exactly that you're not trying to do differently?
If you're try to calculate the average value per column for all the rows - this solution has also been provided as part of the previous thread.
Try to understand the previous solution and/or adjust it to your 'new' [???] requirement.
The difference between now and then is that I need it to be a bash/bourne script. The previous thread was for a awk-program. But I had to change it to be a bash/bourne shell script. To be honest, I don't know the difference really, but I got some uggly looks from my friend when I showed him the awk-program that some of you helped me with. So I turned back to bourne/bash.

So my script so far is:

cat $1 | grep "results" | grep "get numbers" | xargs -n $2

It is somewhat more advanced then the command above, but this is only so you get the whole picture. It produces columns with values from files named $1 and the numbers of columns are determined by $2.

Now, since $2 is a variable I don't know how many columns there is going to be. It is not going to be many, lets say around 2-10 should suffice. I need, as I previous stated go through each column adding them and then dividing them with the number of elements in each column. So if the output from the following command above is:

1 4
2 5
3 6
4 7
5 8

The result should be:

3 6

Which is the average of each of the columns.

I've done something like this, but it doesn't seem to work,

while [ $j > 0 ]
do
cat list | awk '{sum+=$i} END {print sum/NR}'
i=$[$i+1]
j=$[$j-1]
done

awk seems to have problem with $i. But I need this to be a variable but awk says:

awk: illegal field $(), name "i"
input record number 1, file
source line number 1

Last edited by baghera; 08-30-2007 at 03:45 PM..