Awk, appending a number in the first column of a row with a condition

Hi everyone,

I have a data file in which the data is stored in event blocks. What I would like to get is that the same file with every data row starting with the number of event block. So here is two event blocks from my file:

<event>
       -2   -1    0    0    0  501  0.00000000000E+00
        2   -1    0    0  501    0  0.00000000000E+00
       23    2    1    2    0    0  0.17763568394E-14
      -15    2    3    3    0    0  0.15027196484E+02
       15    2    3    3    0    0 -0.15027196484E+02
      -16    1    4    4    0    0  0.61519221046E+01
      -13    1    4    4    0    0  0.80239066744E+01
       14    1    4    4    0    0  0.85136770460E+00
       16    1    5    5    0    0 -0.11503308753E+02
       13    1    5    5    0    0 -0.16847543683E+01
      -14    1    5    5    0    0 -0.18391333621E+01
</event>
<event>
        2   -1    0    0  501    0  0.00000000000E+00
       -2   -1    0    0    0  501  0.00000000000E+00
      -15    2    1    2    0    0 -0.22812990072E+02
       15    2    1    2    0    0  0.22812990072E+02
      -16    1    3    3    0    0 -0.17582975494E+01
      -13    1    3    3    0    0 -0.27110286835E+01
       14    1    3    3    0    0 -0.18343663839E+02
       16    1    4    4    0    0  0.76382745636E+01
       13    1    4    4    0    0  0.45801258260E+01
      -14    1    4    4    0    0  0.10594589682E+02
</event>

after an awk operation I would like to get the following form:

<event>
1       -2   -1    0    0    0  501  0.00000000000E+00
1        2   -1    0    0  501    0  0.00000000000E+00
1       23    2    1    2    0    0  0.17763568394E-14
1      -15    2    3    3    0    0  0.15027196484E+02
1       15    2    3    3    0    0 -0.15027196484E+02
1      -16    1    4    4    0    0  0.61519221046E+01
1      -13    1    4    4    0    0  0.80239066744E+01
1       14    1    4    4    0    0  0.85136770460E+00
1       16    1    5    5    0    0 -0.11503308753E+02
1       13    1    5    5    0    0 -0.16847543683E+01
1      -14    1    5    5    0    0 -0.18391333621E+01
</event>
<event>
2        2   -1    0    0  501    0  0.00000000000E+00
2       -2   -1    0    0    0  501  0.00000000000E+00
2      -15    2    1    2    0    0 -0.22812990072E+02
2       15    2    1    2    0    0  0.22812990072E+02
2      -16    1    3    3    0    0 -0.17582975494E+01
2      -13    1    3    3    0    0 -0.27110286835E+01
2       14    1    3    3    0    0 -0.18343663839E+02
2       16    1    4    4    0    0  0.76382745636E+01
2       13    1    4    4    0    0  0.45801258260E+01
2      -14    1    4    4    0    0  0.10594589682E+02
</event>

thanks for all the help.

Hello hayreter,

Following may help you in same.

awk '/<\/event>/ {print $0;next} /\<event\>/ {print $0;A++;next} !/^$/{print A OFS $0}' Input_file

Output will be as follows.

<event>
1        -2   -1    0    0    0  501  0.00000000000E+00
1         2   -1    0    0  501    0  0.00000000000E+00
1        23    2    1    2    0    0  0.17763568394E-14
1       -15    2    3    3    0    0  0.15027196484E+02
1        15    2    3    3    0    0 -0.15027196484E+02
1       -16    1    4    4    0    0  0.61519221046E+01
1       -13    1    4    4    0    0  0.80239066744E+01
1        14    1    4    4    0    0  0.85136770460E+00
1        16    1    5    5    0    0 -0.11503308753E+02
1        13    1    5    5    0    0 -0.16847543683E+01
1       -14    1    5    5    0    0 -0.18391333621E+01
</event>
<event>
2         2   -1    0    0  501    0  0.00000000000E+00
2        -2   -1    0    0    0  501  0.00000000000E+00
2       -15    2    1    2    0    0 -0.22812990072E+02
2        15    2    1    2    0    0  0.22812990072E+02
2       -16    1    3    3    0    0 -0.17582975494E+01
2       -13    1    3    3    0    0 -0.27110286835E+01
2        14    1    3    3    0    0 -0.18343663839E+02
2        16    1    4    4    0    0  0.76382745636E+01
2        13    1    4    4    0    0  0.45801258260E+01
2       -14    1    4    4    0    0  0.10594589682E+02
</event>

Thanks,
R. Singh

1 Like

The following makes the changes only within level-1 <event> blocks.

awk '/<\/event>/ {--lev} {if (lev>0) {print c,$0} else print} /<event>/ && (++lev==1) {c++}' file