Sharat Chandra
Sharat Chandra

Reputation: 4544

Error with redirection

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

Upvotes: 0

Views: 145

Answers (4)

Sharat Chandra
Sharat Chandra

Reputation: 4544

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

Upvotes: 0

sud03r
sud03r

Reputation: 19749

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.

Upvotes: 2

martin clayton
martin clayton

Reputation: 78105

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

#-------v----------
${file}_${f}_v1.xls

Upvotes: 2

Dennis Williamson
Dennis Williamson

Reputation: 359905

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

Upvotes: 2

Related Questions