Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6366301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:24:14+00:00 2026-05-25T00:24:14+00:00

I have two sets of arrays with a variable number of elements, for instance:

  • 0

I have two sets of arrays with a variable number of elements, for instance:

chain=(B C)

hresname=(BMA MAN NAG NDG)

I am parsing a number of files that may contain elements from the array chain at a given position and elements of the array hresname at a different position (position is always fixed in both cases). This is a sample of the data:

ATOM   5792  CB  MET D 213      49.385  -5.683 125.489  1.00142.66           C  
ATOM   5793  CG  MET D 213      50.834  -5.674 125.990  1.00154.50           C  
ATOM   5794  SD  MET D 213      51.530  -7.337 126.277  1.00164.73           S  
ATOM   5795  CE  MET D 213      52.854  -7.386 125.068  1.00169.73           C  
HETATM 5797  C1  NAG B 323      70.090  50.934 125.869  1.00 86.35           C  
HETATM 5798  C2  NAG B 323      69.687  52.074 126.879  1.00 95.95           C  
HETATM 5799  C3  NAG B 323      68.377  52.740 126.390  1.00 87.65           C  
HETATM 5800  C4  NAG B 323      68.598  53.314 125.014  1.00 83.97           C  

First I need to copy lines starting with ATOM whose 5th column matches each of the elements of the array chain to separate files:

while read pdb ; do

for c in "${chain[@]}" ; do
#if [ ${#chain[@]} -eq 1 ] && \
if [ $(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 well (slow but sure). Both the commented and the uncommented versions work.

Next I want to copy lines that start with HETATM and whose 4th column matches elements of the hresname but only if those lines also match an element form the chain array at cloumn number 5th:

while read pdb ; do

for c in "${chain[@]}" ; do
for h in "${hresname[@]}" ; do
if [ ${#chain[@]} -eq 1 ] && \
   [ $(echo "$pdb" | cut -c1-6) == "HETATM" ] && \
   [ $(echo "$pdb" | cut -c22-23) == "${chain[$c]}" ] \
   [ $(echo "$pdb" | cut -c18-20) == "${hresname[$h]}" ] ; then
echo "$pdb" >> ../../properpdb/${pdbid}_${chain[$c]}.pdb
fi
done
done

done < ${pdbid}.pdb

However, this does not work. I repeatedly receive an error:

line 66: [: too many arguments

Line 66 is:

   [ $(echo "$pdb" | cut -c22-23) == "${chain[$c]}" ] \

Which puzzles me because the error happens even if I restrict the loop to chain arrays containing a single element.

According to other StackOverflow questions, it should be perfectly possible to do this in bash. Any idea what the problem could be?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T00:24:15+00:00Added an answer on May 25, 2026 at 12:24 am

    You for get to add &&, change this line:

    [ $(echo "$pdb" | cut -c22-23) == "${chain[$c]}" ] \
    

    To

    [ $(echo "$pdb" | cut -c22-23) == "${chain[$c]}" ] &&\
    

    Update:
    You have too many errors in the script, I fixed it and it’s now working. I suggest you read the for loop syntax in the bash manual first.

    chain=(B C)                                                                                                                                
    hresname=(BMA MAN NAG NDG)
    while read pdb ; do
        for c in ${chain[@]} ; do
            for h in ${hresname[@]} ; do
                if [ $(echo "$pdb" | cut -c1-6) == "HETATM" ] && \
                   [ $(echo "$pdb" | cut -c22-23) == "$c" ] && \
                   [ $(echo "$pdb" | cut -c18-20) == "$h" ] ; then
                    echo "$pdb" >> ../../properpdb/${pdbid}_${chain[$c]}.pdb
                fi
            done
        done
    done
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two sets of elements with (sometimes) corresponding rel and id attributes: <a
I have two sets of arrays like this for example. $Arr1['uid'][]='user 1'; $Arr1['weight'][]=1; $Arr1['uid'][]='user
I have two arrays with two sets of values in ($a[] and $b[]) I
I have two Sets. Set b is the subset of Set a . they're
I have two sets of data, (Ax, Ay; Bx, By). I'd like to plot
EDIT: I also have access to ESXLT functions. I have two node sets of
I have to generate two random sets of matrices Each containing 3 digit numbers
I have a class that contains two sets. They both contain the same key
I have two arrays of animals (for example). $array = array( array( 'id' =>
I have two arrays of System.Data.DataRow objects which I want to compare. The rows

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.