tags:

views:

68

answers:

4

I'm trying to find average of values in 2nd column in some files. Filenames have following pattern eg:

tau_2.54_even_v1.xls
tau_2.54_odd_v1.xls
tau_1.60_v1.xls
tau_800_v1.xls

The other filenames can be obtained by replacing variable file with oter variables pmb , xhpl etc .. Here is the script I've written .. Can anyone kindly find the error and let me know ?

#!/bin/bash
for file in pmb_mpi tau xhpl mpi_tile_io fftw ; do
    for f in "2.54" "1.60" "800" ;do
    if [ ${f} = 2.54 ]
    then 
        for order in even odd ; do
     echo ${file}_${f}_${order}_v1.xls
     awk 'sum+=$2 ;END {print "Average = " , $sum/NR > ${file}_${f}_${order}_avrg.xls }' ${file}_${f}_${order}_v1.xls
        done
    else
        echo ${file}_${f}_v1.xls 
        awk 'sum+=$2 ;END {print "Average = " , $sum/NR > ${file}_${f}_avrg.xls }' ${file}_{f}_v1.xls
    fi

    done
done
+2  A: 

One problem I see right away is that you have a dollar sign with the variable "sum" in your awk scripts.

Change it to remove the dollar sign. One of the lines would then look like this:

        awk 'sum+=$2 ;END {print "Average = " , sum/NR > ${file}_${f}_${order}_avrg.xls }' ${file}_${f}_${order}_v1.xls
Dennis Williamson
Thanks for correcting that error. The error seems to be in the awk command..When I commented the awk commands, the script did not show any errors .
Sharat Chandra
+2  A: 

In the else clause you've missed a dollar sign in the input file name to awk.

#-------v----------
${file}_${f}_v1.xls
martin clayton
Thanks for correcting that error. But the error seems to be in the awk command..When I commented the awk commands, the script did not show any errors .
Sharat Chandra
+2  A: 

If your xls files are spreadsheets, i dont think you can normally read them using awk. You should convert xls to some convinient file format like csv using a perl module here:

http://search.cpan.org/~ken/xls2csv/script/xls2csv

And now you can use awk over that csv file.

Neeraj
Thanks man Neeraj
Sharat Chandra
A: 

I got my answer. Here is the code

#!/bin/bash

for file in pmb_mpi tau xhpl mpi_tile_io fftw ; do

for f in 2.54  1.60 800 ; do

if [ ${f} = 2.54 ]
then 
    for order in even odd ; do

printf "\n${file}${f}${order}_v1.xls " >> Safe/P-state-summary.xls

 awk '{sum+=$3}; END  {print "\n${file}_${f}_${order}_v1.xls  " sum/NR}' ${file}_${f}_${order}_v1.xls >> Safe/P-state-summary.xls
    done
else

printf "\n${file}_${f}_v1.xls " >> Safe/P-state-summary.xls

    awk '{sum+=$3}; END  {print"\n${file}_${f}_v1.xls "  sum/NR}' ${file}_${f}_v1.xls >>  Safe/P-state-summary.xls
fi
done

done

There were problems wit redirection of awk output . Thank you all for your valuable suggestions

Sharat Chandra