2

I have a heavy text with a pattern as below

it starts with

------------------
MOLECULAR ORBITALS
------------------
                      0         1         2         3         4         5   
                 -238.19873 -238.19679 -238.16140 -238.16079 -145.83297 -27.39277
                   1.00000   1.00000   1.00000   1.00000   1.00000   1.00000
                  --------  --------  --------  --------  --------  --------
  0C   1s        -0.000000 -0.000000 -0.000000 -0.000000  0.000000 -0.000001
  0C   2s         0.000000  0.000000  0.000000  0.000001 -0.000002  0.000025
  0C   3s         0.000003 -0.000001  0.000005 -0.000002 -0.000020  0.000230
   .
   .
   .
  1C   1px        0.000000  0.000000  0.000000 -0.000000 -0.000000  0.000005
  1C   1py        0.000000  0.000000 -0.000000 -0.000000  0.000000  0.000003
  1C   2pz       -0.000000 -0.000000 -0.000000  0.000000 -0.000005  0.000002
   .
   .
   .
 13Mn  1dyz      -0.000000 -0.000000 -0.000000  0.000000 -0.000000 -0.000006
 13Mn  1dx2y2    -0.000000  0.000000  0.000000 -0.000000 -0.000000 -0.000005
 13Mn  1dxy       0.000000 -0.000000 -0.000000 -0.000000 -0.000000  0.000001
 13Mn  2dz2       0.000000  0.000001  0.000000 -0.000000 -0.000001  0.000008
   .
   .
   .
181H   1pz       -0.000000  0.000000 -0.000000 -0.000000  0.000003 -0.000020
181H   1px       -0.000001  0.000000 -0.000000 -0.000000  0.000003 -0.000103
181H   1py        0.000000 -0.000000 -0.000000 -0.000000 -0.000001  0.000013
                      6         7         8         9        10        11   
                 -27.39007 -27.35069 -27.34994 -23.19934 -23.19836 -23.19760
                   1.00000   1.00000   1.00000   1.00000   1.00000   1.00000
                  --------  --------  --------  --------  --------  --------
  0C   1s         0.000000  0.000001  0.000008  0.000001 -0.000003  0.000001
  0C   2s        -0.000003 -0.000038 -0.000147 -0.000013  0.000059 -0.000019
  0C   3s         0.000087 -0.000445 -0.000002  0.000091  0.000201 -0.000215

This is a preview the ... just mean it continues. Here's the actual file. (it's big) I can find this patterns using awk

awk '
/MOLECULAR ORBITALS/ {F=1}

F &&
/^[0-9][A-Z]/ {print}' test.out

now I want to sum up the lines whenever $1=$1(of the next line) and the alphabetic part of the $2 is equal the alphabetic part of $2 for example the first three lines should look like:

0C   1s         0.000003 -0.000001  0.000005 -0.000001  -0.000022  0.000254

All I can think about is arrays at the moment, can anyone suggest a solution? I was thinking about using get line checking the criteria and sum the values up if the criteria is match. But this sounds not clean. Is that the right way to do this?

  • This could be better answered at StackOverflow as it's not realted to Ubuntu. Also, are the relevant lines always adjacent? – choroba Oct 29 '15 at 17:37
  • yes they, are. Probably you are right. – Raymond Ghaffarian Shirazi Oct 29 '15 at 17:39
  • Do you need to use AWK or using other tools is fine? (mainly I'm thinking Perl) – kos Oct 29 '15 at 17:56
  • Also how are fields separated? By that exact number of spaces shown in the example (i.e. a variable number of spaces)? – kos Oct 29 '15 at 18:04
  • no I just place ... to show it just continues. – Raymond Ghaffarian Shirazi Oct 29 '15 at 18:27
2

Here's an awk script:

awk '
function show(){
  if(lastkey=="")return
  printf "%-10s ",lastkey
  for(i=3;i<=NF;i++)printf "%9f ",values[i]
  printf "\n"
}

/MOLECULAR ORBITALS/ {F=1}
F && $1~/[0-9]+[A-Z]+/ { 
 key = $1; subkey=$2; gsub(/[^a-z]/,"",subkey);
 newkey = key " " subkey;
 if(lastkey == newkey)
   for(i=3;i<=NF;i++)values[i] += $i
 else{
  show();
  lastkey = newkey;
  for(i=3;i<=NF;i++)values[i] = $i
 }
}
END{ show(); }
' test.out

The show() function at the start is to allow the last set of values to be printed at eof without duplicating the code. The gsub() removes the digits from the 2nd field ($2). newkey is then the concatenation of the 1st field with this, to test on later lines for similarity. When lines are similar, the fields 3 to the end (NF number of fields) are added to the previous values, else they are printed and zeroed.

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.