3

I am trying to sort the following file

http://pastebin.com/EqA2SUMy.

But the part I am trying to sort is the 11 field and only if the first field is atom. Here is an example

  ATOM     33  CG  ASP A 438       8.283  24.407 131.412  1.00 17.18           C  
 ATOM     34  OD1 ASP A 438       9.153  23.597 131.783  1.00 17.73           O  
 ATOM     35  OD2 ASP A 438       7.530  25.009 132.208  1.00 18.39           O  
 ATOM     36  N   PHE A 439       5.102  23.813 128.903  1.00 12.35           N  
 ATOM     37  CA  PHE A 439       3.680  23.598 129.138  1.00 12.15           C  

I know how to sort a file based on fields. For example for my file it would be

            sort -k11 <\file path >

sort based on the eleventh field but how do I make it sort only if first field is atom b/c there are fields that go to 11 and start with HETATM and such.

  • It looks like the ATOM and HETATM lines are sorted on the 2nd field and are interspersed. Do you need HETATM lines to still be interspersed with ATOM lines after the ATOM lines are sorted? If so, by what sorting criteria? If not, can you separate the file into two files - one with ATOM, one not - then sort the ATOM file, then combine the two files together? – Mark Plotnick Sep 16 '14 at 17:22
  • @MarkPlotnick No I do not need the HETATM lines, only the ATOM. – Fernando Martinez Sep 16 '14 at 17:26
  • The sorting criteria is on the 11 field, it is based on the ascending order. – Fernando Martinez Sep 16 '14 at 17:27
  • The sort list must only be the ATOM – Fernando Martinez Sep 16 '14 at 17:31
2

This should work, if I understood correctly, you only want the lines starting with ATOM.

sort -k11 <(awk '$1 ~ /ATOM/' file)

Explanation:

  • sort -k11: sort by the 11th field
  • awk '$1 ~ /ATOM/' file: only prints the fields startig with ATOM
  • The awk part is the input to the sort part
  • It works!!!!!!!!! – Fernando Martinez Sep 16 '14 at 17:37
  • well actually it does work, it print all the atom fields and the eleventh order in descending order. – Fernando Martinez Sep 16 '14 at 17:38
  • 1
    @FernandoMartinez wasn't that what you wanted? For reverse order use sort -k11 -r. – chaos Sep 16 '14 at 17:40
  • Yes it was, I was describing the output. – Fernando Martinez Sep 16 '14 at 17:42
  • It might be better to use awk '$1=="ATOM"' file so that if you have cases where $1 can be ATOMIC they will be ignored. – terdon Sep 16 '14 at 18:25
1

How would you like the end result to look like? Should all the rows with first field ATOM appear at the top of the file, end of file?

If it's okay to have the ATOMs anywhere in the list, then a possible way is to sort them in a different file and append to top or bottom of the original file, or a specific column. I'm sure someone else will post an easier solution - just my two cents for now..

grep -v ^ATOM file > result; sort -k11 < grep ^ATOM file >> result

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.