开发者

identify error in if statement

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开发者_C百科 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


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.


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.


Can't you just move the redirection?

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜