Define an error in an if statement

Here is the complete code.

I try to do it โ†’

I am trying to find the average of the values โ€‹โ€‹in the second column of files. If the file has 2.54 in its name, I want to find the average values โ€‹โ€‹from the files named file_2.54_even.xls and file_2.54_odd.xls. If fiename doesn't have a 2.54 name in its name, just find the average value of the 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 
0
scripting bash
source share
3 answers

Since your awk command is inside single quotes, the shell variables file and f will not be extended. This is why you get 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 

This will just install flist for both files or one file, depending on whether f 2.54 or not, then click this list of files through one awk script.

+1
source share

I believe that the problem may be that you are comparing integers and strings.

Try

 if [ $(f) -eq 2.54 ] 

If you donโ€™t see the full code, I donโ€™t see which line the error is in.

Edit: try this in cygwin. However, I get no errors.

+1
source share

Can you move the redirect?

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

All Articles