averaging columns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting averaging columns
# 1  
Old 09-02-2011
averaging columns

Hi,

I have a file that has 201 columns (1 for the name of each row and the other 200 are values)

I want to print the average for each column

the file looks like this (as an example this only has 7 columns with values)

Code:
     1  2  3  4  5  6  7
abr  5  6  7  1  2  4  5
hhr  2  1  3  4  2  1  2
iip   1  3  1  1  5  3  2

I want to just print the average of each column while ignoring the first column and first row.

Code:
4  5  5.5  3  4.5  4  4.5

thanks
# 2  
Old 09-02-2011
Quote:
Originally Posted by kylle345
...
Code:
     1  2  3  4  5  6  7
abr  5  6  7  1  2  4  5
hhr  2  1  3  4  2  1  2
iip   1  3  1  1  5  3  2

I want to just print the average of each column while ignoring the first column and first row.
...
Code:
$
$
$ cat data.txt
     1  2  3  4  5  6  7
abr  5  6  7  1  2  4  5
hhr  2  1  3  4  2  1  2
iip  1  3  1  1  5  3  2
$
$
$ perl -lane 'if ($. > 1) {
                $n++;
                foreach $i (1..$#F) { push @{$x{$i}}, $F[$i] }}
              END {
                foreach $k (sort keys %x) {
                  $sum += $_ foreach @{$x{$k}};
                  $str .= sprintf("%.4f ", $sum/$n);
                  $sum = 0;
              }
              print $str}
             ' data.txt
2.6667 3.3333 3.6667 2.0000 3.0000 2.6667 3.0000
$
$
$

tyler_durden
# 3  
Old 09-02-2011
In awk:

Code:
lines=`cat data.txt | wc -l`  #corrected, thanks to itkamaraj
lines=$((lines-1))
for i in $(seq 2 201); do
 awk -v c=$i -v l=$lines 'NR>1 {s+=$c}; END {printf ("%f ", s/l)}' data.txt
done
echo

and maybe even simplier Smilie without calculating lines before
Code:
for i in $(seq 2 201); do
 awk -v c=$i 'NR>1 {s+=$c;l+=1}; END {printf ("%f ", s/l)}' data.txt
done
echo


Last edited by sulti; 09-02-2011 at 03:35 AM..
# 4  
Old 09-02-2011
??????????

Code:
 
lines=`echo data.txt | wc -l`

# 5  
Old 09-02-2011
Quote:
Originally Posted by itkamaraj
??????????

Code:
 
lines=`echo data.txt | wc -l`

Sorry, my bad Smilie didn't drink my morning coffee Smilie
I meant
Code:
lines=`cat data.txt | wc -l`

but then I've added simplier solution.
# 6  
Old 09-02-2011
And one more thing, you dont want to use the cat command here.

this is called as useless use of cat Smilie http://partmaps.org/era/unix/award.html

Code:
 
wc -l < data.txt
or
wc -l data.txt

# 7  
Old 09-02-2011
I have to disagree Smilie
Code:
$ wc -l test
4 test
$ cat test | wc -l
4

That's why I used cat.
Code:
$ a=`wc -l test`
$ [ $a -gt 0 ] && echo Works
bash: [: Too many arguments

edit:
Ok, I noticed that wc -l < data.txt actualy would work. But wc -l data.txt would not. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Averaging 3 files

Hi, I am trying to average the values from 3 files with the same format. They are very large files so I will describe the file and show some it of. Basically the file has 83 columns (with nearly 7000 rows). The first three columns are the same for each file while the remaining 80 are values... (3 Replies)
Discussion started by: kylle345
3 Replies

2. Shell Programming and Scripting

Averaging help in awk

Hi all, I have a data file like below, where Time is in the second column DATE TIME FRAC_DAYS_SINCE_JAN1 2011-06-25 08:03:20.000 175.33564815 2011-06-25 08:03:25.000 175.33570602... (10 Replies)
Discussion started by: gd9629
10 Replies

3. Shell Programming and Scripting

Averaging Data From Multiple Columns, Using Header if Possible

Hi, I have a file with multiple tab delimited columns and I would like to have the average of each column: Iteration Tree No Lh HMean 1000 1 -78.834717 -78.834717 1100 1 -77.991031 -78.624046 1200 1 -79.416055 -78.761861 1300 1 -79.280494 -78.968099 1400 1 -82.846275 -80.808696 ... (4 Replies)
Discussion started by: mikey11415
4 Replies

4. UNIX for Dummies Questions & Answers

Averaging 100 columns together line by line

Hi I have 100 xy graphs and need to average these together in a line by line fashion. The value of the x axis are the same. y differs e.g. taking only 2 graphs: graph 1 x y 1 3 2 5 3 7 4 9 5 11 graph 2 x y 1 4 2 6 3 10 (2 Replies)
Discussion started by: jenjen_mt
2 Replies

5. UNIX for Dummies Questions & Answers

Averaging the rows using 'awk'

Dear all, I have the data in the following format. I want to do average of each NR= 5 (rows) for all the 3 ($1,$2, $3) columns and want to print average result in another file in the same format. I dont know how to write code for this in 'awk', can some one help me to write a code for this in... (1 Reply)
Discussion started by: arvindr
1 Replies

6. Shell Programming and Scripting

Averaging segments and including the name

Hello, I have a awk line that averages rows. So if my file looks like this: 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 awk -F'\t' -v r=3... (3 Replies)
Discussion started by: phil_heath
3 Replies

7. UNIX for Dummies Questions & Answers

Averaging

Hello all, I'm trying to perform an averaging procedure which selects a selection of rows, average the corresponding value, selects the next set of rows and average the corresponding values etc. The data below illustrates what I want to do. Given two columns (day and value), I want to... (2 Replies)
Discussion started by: Muhammad Rahiz
2 Replies

8. Shell Programming and Scripting

Averaging multiple columns

Hello, I am trying to average multiple columns simultaneously while skipping the first column. I am using this awk line to average one column awk '{sum+=$3} END { print "Average = ",sum/NR}' But I want to be able to do it for multiple columns while skipping the first column. There... (4 Replies)
Discussion started by: gisele_l
4 Replies

9. Shell Programming and Scripting

Averaging segments

Hi, I have a file that I want to average. So specifically I want to average every third column for each row. Here is an example of my file 2 2 2 3 3 3 1 1 1 5 5 5 Heres what I want it to look like after averaging every third column 2 3 1 5 thanks (11 Replies)
Discussion started by: kylle345
11 Replies

10. Shell Programming and Scripting

AWK - averaging $3 by info in $1

Hello, I have three columns of data of the format below: <name> <volume> <size> a 2 1.2 a 2 1.1 b 3 1.7 c 0.7 1.9 c 0.7 1.9 c 0.7 1.8 What I... (3 Replies)
Discussion started by: itisthus
3 Replies
Login or Register to Ask a Question