awk or a combination of commands to read and calculate nth lines from pattern

Two numerical lines, found by either header line, need to be added
and the total placed in a new-header section. Also the total should
should be rounded or cut to a two decimal anynumber.XX format
with the AB string added on the end.

For example: The numerical lines from headers 2 and 3 are to be added together
and placed in the 4th line of the new-header grouping.
The new line would show 13.24 AB

The data file:
Each grouping contains 4 lines.
The line numbers may change every time the data file is accessed.
The numerical lines may or may not have a type (AB in this example) listed after the number.
The third line in the group, the info line, may or may not be the same.

Data file:

header1
header1 - description
info-abc
1.23 AB
header2
header2 - description
info-def
10.678 AB
header3
header3 - description
info-abc
2.5678

A new section would then be added to the end of the data with no empty line above:

new-header
new-header - description
info-xyz
13.24 AB

Hello jessandr,

Not that much clear, please do mention why 1st value of header is NOT being picked up?

Thanks,
R. Singh

Welcome to the forum.

Please become accustomed to supplying sufficient info on and context of your problem. On top of what RavinderSingh13 already said:

  • Where (in a larger file) should the new record be placed?
  • How should the new info line ("info-xyz" in your sample record) be constructed?
  • What be the header name if more than one new headers result?

Start with:

awk '
1
! (NR % 4) {if (length($2)) type=$2;}
! (NR % 12) || ! ((NR + 4) % 12) {sum+=$1;}
! (NR % 12) {
   print "new-header";
   print "new-header - description";
   print "info-xyz";
   printf "%.2f %s\n", sum, type;
}
' data_file