tags:

views:

99

answers:

6

Here is the complete code..

What I'm trying to do is this - >

I'm trying to find the average of the values in the second column of the files . If the file has 2.54 in its name, I want to find averages from files called file_2.54_even.xls and file_2.54_odd.xls . If the fiename does not have 2.54 in its name, then simply find the average of file. For example: file_1.60.xls

#!/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 
     echo ${file}_${f}_even_v1.xls
     awk 'sum+=$2 ;END {print "Average = " , $sum/NR > ${file}_${f}_avrg.xls }' ${file}_${f}_even_v1.xls
     echo ${file}_${f}_odd_v1.xls
     awk 'sum+=$2  ;END {print "Average = " , $sum/NR > ${file}_${f}_avrg.xls }' ${file}_${f}_odd_v1.xls
 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
A: 

Are you missing a ; after the closing bracket?

BjoernD
I doubt it; the semicolon is a separator for when you have `if` and `then` on the same line - it's not needed if they're on separate lines.
paxdiablo
A: 

Works for me in my version of bash (3.2, cygwin). But try quoting the ${f}.

the_mandrill
+1  A: 

I believe the problem may be you're comparing integers and strings.

Try

if [ $(f) -eq 2.54 ]

Although not seeing the full code I can't see what line the error is on.

Edit: Trying it in cygwin I get no error however.

tjmoore
A: 

The only possibility to get this error message is using -eq instead of =.

You asked the question showing the wrong code (and incomplete, too, regarding the awk messages).

-1 on question

TheBonsai
+1  A: 

Because your awk command is inside single quotes, the shell variables file and f will not be expanded. That's why you're getting the awk errors.

I would use the following script:

#!/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
      flist=${file}_${f}_even_v1.xls ${file}_${f}_odd_v1.xls
    else
      flist=${file}_${f}_v1.xls
    fi
    cat ${flist} | awk '
      s+=$2;
      END {print "Average = ",$s/NR}
      ' >${file}_${f}_avrg.xls
    # Or use awk '...' ${flist} >${file}_${f}_avrg.xls
    # if you're overly concerned about efficiency of processes.
  done
done

That will simply set flist to either both files or one file, depending on whether f is 2.54 or not, then push that list of files through a single awk script.

paxdiablo
Useless use of `cat` - `awk` will accept multiple files via your `$flist` variable as an argument: `awk '...' ${flist} > ${file}...`
Dennis Williamson
I know it's useless in this case but that has little to do with the question - you may just as well complain about the indentation style or multi-line awk script :-) I tend to do all my pipelines the same (starting with cat) since not all commands allow the sort of expressiveness you're talking about.
paxdiablo
PAxdiablo.. u have hit the nail on the head . Congrats !
Sharat Chandra
A: 

Can't you just move the redirection?

 awk 'sum+=$2 ;END {print "Average = " , sum/NR }' ${file}_${f}_even_v1.xls > ${file}_${f}_avrg.xls
Dennis Williamson