#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    1
    Rep Power
    0

    Please help to fix awk script


    Good morning, fellows. I would need to ask for your help in editing my awk script. Here is the original version:

    Code:
    BEGIN { printf ("CRYST1   200.000   200.000   200.000  90.00  90.00  90.00 P 1           1\n")
          maxatoms=1000
          natom=0
          found_struct = 0
          found_bond   = 0
          }
    {
       if( NF == 5 )
       {
                  foundff=0
                  natom++
                  fftype[natom]="UNKNOWN"
                  if ($1 ~ /CT/)
                    {
                       fftype[natom] = "C"
                       foundff=1
                    }
                  else if ($1 ~ /OH/)
                    {
                       fftype[natom] = "O"
                       foundff=1
                    }
                  else if ($1 ~ /HC/)
                    {
                       fftype[natom] = "H"
                       foundff=1
                    }
                    else if ($1 ~ /N/)
                    {
                       fftype[natom] = "N"
                       foundff=1
                    }
    
                  else if ($1 ~ /H1/)
                    {
                       fftype[natom] = "H"
                       foundff=1
                    }
                  else if ($1 ~ /HO/)
                    {
                       fftype[natom] = "H"
                       foundff=1
                    }
                  else if ($1 = "C")
                    {
                       fftype[natom] = "C"
                       foundff=1
                    }
                  else if ($1 = "O")
                    {
                       fftype[natom] = "O"
                       foundff=1
                    }
    
                  next
    
                  x[natom] = $1
                  y[natom] = $2
                  z[natom] = $3
    
    
                  if  (foundff == 0)
                    printf("PROBLEM : Atom ff type %s not known\n", $6)
      }
    
    }
    
    END {
             for (iatom=1; iatom <= natom; iatom++)
              {
                printf("HETATM %d %2s %d %14.9f %14.9f %14.9f\n" ,
                      iatom, fftype[iatom], iatom,  x[iatom], y[iatom], z[iatom])
              }
             printf ("END\n")
        }

    And this is type of file I am working with.

    0 3 186 200 75202
    timestep 500 186 0 3 0.002000 1.000000
    40.0000000000 0.0000000000 0.0000000000
    -0.0000000034 40.0000000000 0.0000000000
    -0.0000000034 -0.0000000034 40.0000000000
    CT_1 1 12.011000 0.061000 1.087513
    -1.961325738 1.828501682 -8.933652557
    CT_1 2 12.011000 0.061000 0.789711
    -3.851025437 3.495427316 -10.05849230
    CT_1 3 12.011000 0.061000 0.581330
    -5.804493575 4.589489777 -8.369482861

    ect

    I would like to get this as an output:

    CRYST1 200.000 200.000 200.000 90.00 90.00 90.00 P 1 1
    HETATM 1 C 1 -1.961325738 1.828501682 -8.933652557
    HETATM 2 C 2 -3.851025437 3.495427316 -10.05849230
    HETATM 3 C 3 -5.804493575 4.589489777 -8.369482861

    ect

    But coordinates are not really picking up well (next line after CT_1 1 12.011000 0.061000 1.087513). Can you please have a look and suggest any solutions. Thanks.
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,366
    Rep Power
    1870
    > else if ($1 = "C")
    > else if ($1 = "O")
    These should be ==, not =

    > next
    This starts with the next record. All the lines following it never execute.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper

IMN logo majestic logo threadwatch logo seochat tools logo