0

I am trying to create a bash script that will provide me with the total number of atoms at the beginning of the file, while also deleting the second count of atoms. However I am unsure how to go about it, since I can't go by column and the line the second value is can vary. Any tips would be much appreciated. An example for this set of molecules would be great too. Thank you in advance.

For example:

18
     H           -1.246549   -3.377143   -1.800000
     C           -0.707293   -2.432587   -1.800000
     C           -1.403477   -1.241616   -1.800000
     C            0.707293   -2.432587   -1.800000
     C           -0.716360    0.000000   -1.800000
     H           -2.492687   -1.240767   -1.800000
     C            1.403477   -1.241616   -1.800000
     H            1.246549   -3.377143   -1.800000
     C            0.716360    0.000000   -1.800000
     C           -1.403477    1.241616   -1.800000
     H            2.492687   -1.240767   -1.800000
     C            1.403477    1.241616   -1.800000
     H           -2.492687    1.240767   -1.800000
     C           -0.707293    2.432587   -1.800000
     C            0.707293    2.432587   -1.800000
     H            2.492687    1.240767   -1.800000
     H           -1.246549    3.377143   -1.800000
     H            1.246549    3.377143   -1.800000
17
     C           -2.432587    0.707293    1.800000
     C           -1.241616    1.403477    1.800000
     C            0.000000    0.716360    1.800000
     C            0.000000   -0.716360    1.800000
     C           -1.241616   -1.403477    1.800000
     C           -2.432587   -0.707293    1.800000
     H            1.240767    2.492687    1.800000
     H           -3.377143    1.246549    1.800000
     H           -1.240767    2.492687    1.800000
     C            1.241616    1.403477    1.800000
     C            1.241616   -1.403477    1.800000
     H           -1.240767   -2.492687    1.800000
     H           -3.377143   -1.246549    1.800000
     C            2.432587   -0.707293    1.800000
     C            2.432587    0.707293    1.800000
     H            1.240767   -2.492687    1.800000
     H            3.377143   -1.246549    1.800000

To turn that previous file into a file like what I have shown below by adding the sum of the atoms together, while also deleting the second number.

35
18
     H           -1.246549   -3.377143   -1.800000
     C           -0.707293   -2.432587   -1.800000
     C           -1.403477   -1.241616   -1.800000
     C            0.707293   -2.432587   -1.800000
     C           -0.716360    0.000000   -1.800000
     H           -2.492687   -1.240767   -1.800000
     C            1.403477   -1.241616   -1.800000
     H            1.246549   -3.377143   -1.800000
     C            0.716360    0.000000   -1.800000
     C           -1.403477    1.241616   -1.800000
     H            2.492687   -1.240767   -1.800000
     C            1.403477    1.241616   -1.800000
     H           -2.492687    1.240767   -1.800000
     C           -0.707293    2.432587   -1.800000
     C            0.707293    2.432587   -1.800000
     H            2.492687    1.240767   -1.800000
     H           -1.246549    3.377143   -1.800000
     H            1.246549    3.377143   -1.800000
     C           -2.432587    0.707293    1.800000
     C           -1.241616    1.403477    1.800000
     C            0.000000    0.716360    1.800000
     C            0.000000   -0.716360    1.800000
     C           -1.241616   -1.403477    1.800000
     C           -2.432587   -0.707293    1.800000
     H            1.240767    2.492687    1.800000
     H           -3.377143    1.246549    1.800000
     H           -1.240767    2.492687    1.800000
     C            1.241616    1.403477    1.800000
     C            1.241616   -1.403477    1.800000
     H           -1.240767   -2.492687    1.800000
     H           -3.377143   -1.246549    1.800000
     C            2.432587   -0.707293    1.800000
     C            2.432587    0.707293    1.800000
     H            1.240767   -2.492687    1.800000
     H            3.377143   -1.246549    1.800000


1

With tac (print files in reverse) and awk

tac file | awk '$1~/^[0-9]+$/{sum+=$1; curr=$0; next} {print} END{print curr; print sum}' | tac

$1~/^[0-9]+$/ uses a regular expression (^[0-9]+$) to check whether the first column contains a number.

Update:

awk executes $1~/^[0-9]+$/{sum+=$1; curr=$0; next} {print} for every line of your file.

If first column contains a number then awk adds up the numbers in variable sum and saves current line/row ($0) in variable curr and stops executing code for current line/row here because of next. This means that the print command is not executed in this case.

If first column contains no number then only print is executed.

A different notation for $1~/^[0-9]+$/{sum+=$1; curr=$0; next} {print} is this:

{
  if($1~/^[0-9]+$/)
    {sum+=$1; curr=$0}
  else
    {print}
}

When the last line of your file is processed, code in END section is executed once and awk ends afterwards.


See: https://stackoverflow.com/tags/awk/info

  • Could you explain what the "next} {print} END{print curr; print sum}' " does? Since google tells me that next just stops the awk command and "Awk will execute the action(s) specified in END before it actually exits". I am not sure what that means, as in next seems to skip the rest of the command but does END run before the next command? – Tony Aug 16 '20 at 13:54
  • I've updated my answer. I hope the explanation helps you. – Cyrus Aug 16 '20 at 14:14

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.