Pergunta

So i am trying to write a bash script to check if all values in a data set are within a certain margin of the average.

so far:

    #!/bin/bash
cat massbuild.csv
while IFS=, read col1 col2
do
x=$(grep "$col2" $col1.pdb | grep "HETATM" | awk '{ sum += $7; n++ } END { if (n > 0) print sum / n; }')


i=$(grep "$col2" $col1.pdb | grep "HETATM" | awk '{print $7;}')


if $(($i > $[$x + 15])); then
echo "OUTSIDE THE RANGE!"
fi

done < massbuild.csv

So far, I have broken it down by components to test, and have found the values of x and i read correctly, but it seems that adding 15 to x, or the comparison to i doesn't work.

I have read around online and i am stumped =/

Foi útil?

Solução

Without sample input and expected output we're just guessing but MAYBE this is the right starting point for your script (untested, of course, since no in/out provided):

#!/bin/bash
awk -F, '
NR==FNR {
    file = $1 ".pdb"
    ARGV[ARGC] = file
    file2col2s[file] = (col1to2s[file] ? file2col2s[file] FS : "") $2
    next
}

FNR==1 { split(file2col2s[FILENAME],col2s) }

/HETATM/ {
    for (i=1;i in col2s;i++) {
        col2 = col2s[i]
        if ($0 ~ col2) {
            sum[FILENAME,col2] += $7
            cnt[FILENAME,col2]++
        }
    }
}

END {
    for (file in file2col2s) {
        split(file2col2s[file],col2s)
        for (i=1;i in col2s;i++) {
            col2 = col2s[i]
            print sum[file,col2]
            print cnt[file,col2]
        }
    }
}
' massbuild.csv

Outras dicas

Does this help?

a=4; b=0; if [ "$a" -lt "$(( $b + 5 ))" ]; then echo "a < b + 5"; else echo "a >= b + 5"; fi

Ref: http://www.tldp.org/LDP/abs/html/comparison-ops.html

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top