mirix
mirix

Reputation: 523

Bash array within a double loop (while/for/if)

I am parsing a number of files and copying the lines matching two conditions to a new file. The script behaves differently depending on the size of the array:

while read pdb ; do
# for c in "${chain[@]}" ; do
for ((c=0; c<${#chain[@]}; c++)) ; do
if [ ${#chain[@]} -eq 1 ] && [ "$(echo "$pdb" | cut -c1-4)" == "ATOM" ] && [ "$(echo "$pdb" | cut -c22-23)" == "${chain[$c]}" ] ; then
echo "$pdb" >> ../../properpdb/${pdbid}_${chain[$c]}.pdb
fi
done
done < ${pdbid}.pdb

This works, but only if I remove the last condition (the one referring to a particular array element). I have tried many different syntaxes (double square brackets, using ${c} rather than ${chain[$c]}, etc.) to no avail.

This is ${pdbid}.pdb

ATOM     13  CA  SER A   9     107.761  75.138  27.465  1.00 24.92           C  
ATOM     14  C   SER A   9     107.081  73.915  26.851  1.00 21.25           C  
ATOM     15  O   SER A   9     105.984  73.987  26.313  1.00 24.75           O  
ATOM     16  CB  SER A   9     107.956  76.218  26.399  1.00 30.66           C  

This is one of the arrays:

chain=(A)

Debug:

+ read pdb
+ for c in '"${chain[@]}"'
+ '[' 1 -eq 1 ']'
++ echo 'ATOM   1916  CZ3 TRP B  43     -15.691  19.837  49.406  1.00 12.45           C'
++ cut -c1-4
+ '[' ATOM == ATOM ']'
++ echo 'ATOM   1916  CZ3 TRP B  43     -15.691  19.837  49.406  1.00 12.45           C'
++ cut -c22-23
+ [[ A == B  ]]
+ read pdb

Everything seems to be right except that the last condition is not surrounded by single quotes. There is no output even if A == A. If I remove that condition, it works.

Upvotes: 1

Views: 680

Answers (1)

Mu Qiao
Mu Qiao

Reputation: 7107

The problem is you cut two characters in the test expression, it should be:

if [ ${#chain[@]} -eq 1 ] && \
   [ $(echo "$pdb" | cut -c1-4) == "ATOM" ] && \
   [ $(echo "$pdb" | cut -c22-22) == "${chain[$c]}" ]; then     
    echo "$pdb" >> ${pdbid}_${chain[$c]}.pdb
fi

You should use cut -c22-22 rather than -c22-23. I removed the double quotes around the command substitution so it will also remove white spaces for you.

Upvotes: 1

Related Questions