Changing a text file

I have a file as below and want to change it using awk

I want to find the entries such as

Iteration No.788
Best Value        0.00408152
Next-Worst Value  0.00522935
Worst Value       0.00523487

and change it to

Iteration No.788
788.   Best Value       = 0.00408152
788.   Next-Worst Value = 0.00522935
788.   Worst Value      = 0.00523487
***********************************************
TSIMPLEX V1001
Non-linear Local Inversion using Simplex Algorithm
**********************************************

Parameter Reading

Base model = m04.base

Reading data file (.dat): P PHASES/SOURCES/DATA Done
DTau = 0.03
MDAcc = 0.1
Mindist = 0.05
Maxitertp = 25
Sigma0 = 1
Travel Time Data file = m04-npt06-sr40-syn.dat
Number of layers = 1

Parametrization of Layer n. 1
P:
NxP = 16
NzP = 12
NxS = 2
NzS = 2
Ni = 2
IntI = LIN
IntP = LIN
IntS = LIN
VarI = -1
VarP = 0.03
VarS = -1
PDegP = 1
PDegS = 1
PDegI = 1

Inmod = m04-npt06-sr40-syn-dp01-16x12drw.vmod
Out file = m04-npt06-sr40-syn-dv0p03-16x12smp.vmod
Expl file = m04-npt06-sr40-syn-dv0p03-16x12smp.expl
Backup file = m04-npt06-sr40-syn-dv0p03-16x12smp.bck
MaxIter = 2000
Tol = 0

Starting SIMPLEX for minimization
Building Initial 193 Vertex Simplex
Initial Simplex

Shot n.1 Shot n.2 Shot n.3 Shot n.4 Shot n.5 Shot n.6 Shot n.7 Shot n.8 Shot n.9
Shot n.1 Shot n.2 Shot n.3 Shot n.4 Shot n.5 Shot n.6 Shot n.7 Shot n.8 Shot n.9
Shot n.1 Shot n.2 Shot n.3 Shot n.4 Shot n.5 Shot n.6 Shot n.7 Shot n.8 Shot n.9
Shot n.1 Shot n.2 Shot n.3 Shot n.4 Shot n.5 Shot n.6 Shot n.7 Shot n.8 Shot n.9
Shot n.1 Shot n.2 Shot n.3 Shot n.4 Shot n.5 Shot n.6 Shot n.7 Shot n.8 Shot n.9
Shot n.1 Shot n.2 Shot n.3 Shot n.4 Shot n.5 Shot n.6 Shot n.7 Shot n.8 Shot n.9
Shot n.1 Shot n.2 Shot n.3 Shot n.4 Shot n.5 Shot n.6 Shot n.7 Shot n.8 Shot n.9
Shot n.1 Shot n.2 Shot n.3 Shot n.4 Shot n.5 Shot n.6 Shot n.7 Shot n.8 Shot n.9

Iteration No.785
Best Value        0.00408152
Next-Worst Value  0.00523954
Worst Value       0.00524134
Reflection
Shot n.1 Shot n.2 Shot n.3 Shot n.4 Shot n.5 Shot n.6 Shot n.7 Shot n.8 Shot n.9
Shot n.1 Shot n.2 Shot n.3 Shot n.4 Shot n.5 Shot n.6 Shot n.7 Shot n.8 Shot n.9
Replace W with R
RTol = 0.248474

Iteration No.786
Best Value        0.00408152
Next-Worst Value  0.00523606
Worst Value       0.00523954
Reflection
Shot n.1 Shot n.2 Shot n.3 Shot n.4 Shot n.5 Shot n.6 Shot n.7 Shot n.8 Shot n.9
Negative contraction
Replace W with Cn
RTol = 0.24782

Iteration No.787
Best Value        0.00408152
Next-Worst Value  0.00523487
Worst Value       0.00523606
Reflection
Shot n.1 Shot n.2 Shot n.3 Shot n.4 Shot n.5 Shot n.6 Shot n.7 Shot n.8 Shot n.9
Negative contraction
Replace W with Cn
RTol = 0.247595

Iteration No.788
Best Value        0.00408152
Next-Worst Value  0.00522935
Worst Value       0.00523487
Reflection
Shot n.1 Shot n.2 Shot n.3 Shot n.4 Shot n.5 Shot n.6 Shot n.7 Shot n.8 Shot n.9
Shot n.1 Shot n.2 Shot n.3 Shot n.4 Shot n.5 Shot n.6 Shot n.7 Shot n.8 Shot n.9
Replace W with R
RTol = 0.246558

Try this:

awk 'f-->0{printf("%s.\t%s%s\t= %s\n",n,$1,$2,$3);next}
/Iteration/{n=substr($2,4); f=3}1' file

It works thanks.

How can I include this in an awk script?

I understand the "printf" and "substr($2,4)" but can't figure out

"f-->0" and "f=3" thing

What script? What are trying to achieve?

f=3

If a line matches with /Iteration/ we set a counter f to format the next 3 lines with the code of the 1st line.

f-->0 {printf("%s.\t%s%s\t= %s\n",n,$1,$2,$3);next}

if f > 0 substract 1 from f and perform the actions between {..}

I understand now.

Yes I am trying to put the awk commands in a file then call it using

awk -f file.awk file.txt > fout.txt

file.awk:

f-->0{printf("%s.\t%s%s\t= %s\n",n,$1,$2,$3);next}
/Iteration/{n=substr($2,4); f=3}1

Just one thing, the 1 at the end, does than mean

{ print }

yes

#!/bin/bash

declare -i f
while read -r LINE
do
  case "$LINE"  in
    "Iteration"*)
        num="${LINE##*.}"
        f=1
        echo "$LINE"
        continue
        ;;
    "") f=0 ;;
  esac
  [[ "$f" == 1 ]] && LINE="$num $LINE"
  echo "$LINE"
done <"file"