Como usar o awk para dividir um arquivo usando um padrão de texto

1

Eu tenho um arquivo de 2Gb. Isso tem um cabeçalho e muitos muitos "eventos" -estruturas. É assim que o começo vai parecer:

<run example>
<header>
 5
   This is header                                                   
</header>

<event  =             22>
<evhead>
 8
       3       1       2       0       0       0       0       0
       0       0       1       0       1      30       0       1
       4       1       4       3       1       0       1       0
       0       0       0       0       0       1       1       8
       0       1       0       2       1       5       2       0
       2       1       3       7       3       1       1       0
       1       0   10100       2       3       1       5       1
       1       5       1       7       2       3       2       2
</evhead>
 0 97
           3      11       0       0       3       4  1.94791176123E-14  0.00000000000E+00 -2.75000000000E+01  2.75000000047E+01  5.10000000000E-04
                                                      0.00000000000E+00  0.00000000000E+00  0.00000000000E+00  0.00000000000E+00
           3    2212       0       0       5       0 -1.94791176123E-14  0.00000000000E+00  9.20000000000E+02  9.20000478451E+02  9.38270000000E-01
                                                      0.00000000000E+00  0.00000000000E+00  0.00000000000E+00  0.00000000000E+00
           3      11       1       0       0       0  4.63012694434E+00  2.62561831936E+00 -2.31855757639E+01  2.37887130977E+01  5.10000000000E-04
                                                      0.00000000000E+00  0.00000000000E+00  0.00000000000E+00  0.00000000000E+00
           3      22       1       0       0       0 -4.63012694434E+00 -2.62561831936E+00 -4.31442423592E+00  3.71128690719E+00 -5.75956188088E+00
                                                      0.00000000000E+00  0.00000000000E+00  0.00000000000E+00  0.00000000000E+00
           3    2212       2       0       0       0 -2.16995636615E-14 -1.11022302463E-15  9.20000000000E+02  9.20000478451E+02  9.38270000000E-01
                                                      0.00000000000E+00  0.00000000000E+00  0.00000000000E+00  0.00000000000E+00
           3      22       4       0       0       0 -4.60626572550E+00 -2.61208727495E+00 -2.23619853289E+00  5.74815342040E+00  0.00000000000E+00
                                                      0.00000000000E+00  0.00000000000E+00  0.00000000000E+00  0.00000000000E+00
</event>

O arquivo inteiro contém 97000 desses blocos de "evento". Então eu quero dividir este arquivo em 10 arquivos da maneira que cada um conteria o cabeçalho E 10 000 dos blocos "evento". Todos os blocos diferem por índice (eles são aleatórios). O último arquivo, é claro, contém apenas 7 000 blocos.

Eu tentei várias instruções da pilha como aqui: link link mas não funcionou para mim.

Abaixo está um exemplo um pouco maior do arquivo para quaisquer testes ( file_to_download ):

<run example>
<header>
 5
   header                                                     
</header>

<event  =             22>
<evhead>
 8
       3       1       2       0       0       0       0       0
       0       0       1       0       1      30       0       1
       4       1       4       3       1       0       1       0
</evhead>
 0 97
           3      11       0       0       3       4  1.94791176123E-14  0.00000000000E+00 -2.75000000000E+01  2.75000000047E+01  5.10000000000E-04
                                                      0.00000000000E+00  0.00000000000E+00  0.00000000000E+00  0.00000000000E+00
           3    2212       0       0       5       0 -1.94791176123E-14  0.00000000000E+00  9.20000000000E+02  9.20000478451E+02  9.38270000000E-01
</event>

<event  =             26>
<evhead>
 8
       3       1       2       0       0       0       0       0
       0       0       1       0       1      30       0       1
       4       1       4       3       1       0       1       0
</evhead>
 0 52
           3      11       0       0       3       4  1.94791176123E-14  0.00000000000E+00 -2.75000000000E+01  2.75000000047E+01  5.10000000000E-04
                                                      0.00000000000E+00  0.00000000000E+00  0.00000000000E+00  0.00000000000E+00
           3    2212       0       0       5       0 -1.94791176123E-14  0.00000000000E+00  9.20000000000E+02  9.20000478451E+02  9.38270000000E-01
</event>

<event  =             31>
<evhead>
 8
       3       1       2       0       0       0       0       0
       0       0       1       0       1      30       0       1
       4       1       4       3       1       0       1       0
       0       0       0       0       0       1       1       8
</evhead>
 0 92
           3      11       0       0       3       4  1.94791176123E-14  0.00000000000E+00 -2.75000000000E+01  2.75000000047E+01  5.10000000000E-04
                                                      0.00000000000E+00  0.00000000000E+00  0.00000000000E+00  0.00000000000E+00
           3    2212       0       0       5       0 -1.94791176123E-14  0.00000000000E+00  9.20000000000E+02  9.20000478451E+02  9.38270000000E-01
                                                      0.00000000000E+00  0.00000000000E+00  0.00000000000E+00  0.00000000000E+00
           3      11       1       0       0       0  4.39003604933E+00  4.97037860337E+00 -2.04926313413E+01  2.15389187176E+01  5.10000000000E-04
</event>

<event  =             37>
<evhead>
 8
       3       1       2       0       0       0       0       0
       0       0       1       0       1      30       0       1
</evhead>
 0 77
           3      11       0       0       3       4  1.94791176123E-14  0.00000000000E+00 -2.75000000000E+01  2.75000000047E+01  5.10000000000E-04
                                                      0.00000000000E+00  0.00000000000E+00  0.00000000000E+00  0.00000000000E+00
           3    2212       0       0       5       0 -1.94791176123E-14  0.00000000000E+00  9.20000000000E+02  9.20000478451E+02  9.38270000000E-01
                                                      0.00000000000E+00  0.00000000000E+00  0.00000000000E+00  0.00000000000E+00
           3      11       1       0       0       0  7.91768942174E+00  3.75815788575E+00 -2.09569980000E+01  2.27158385693E+01  5.10000000000E-04
                                                      0.00000000000E+00  0.00000000000E+00  0.00000000000E+00  0.00000000000E+00
</event>

<event  =             41>
<evhead>
 8
       3       1       2       0       0       0       0       0
       0       0       1       0       1      30       0       1
       4       1       4       3       1       0       1       0
</evhead>
 0 122
           3      11       0       0       3       4  1.94791176123E-14  0.00000000000E+00 -2.75000000000E+01  2.75000000047E+01  5.10000000000E-04
                                                      0.00000000000E+00  0.00000000000E+00  0.00000000000E+00  0.00000000000E+00
           3    2212       0       0       5       0 -1.94791176123E-14  0.00000000000E+00  9.20000000000E+02  9.20000478451E+02  9.38270000000E-01
                                                      0.00000000000E+00  0.00000000000E+00  0.00000000000E+00  0.00000000000E+00
           3      11       1       0       0       0 -3.63469912393E+00  3.95372353695E+00 -1.62133507727E+01  1.70796870892E+01  5.10000000000E-04
                                                      0.00000000000E+00  0.00000000000E+00  0.00000000000E+00  0.00000000000E+00
</event>
    
por greyxray 02.10.2016 / 20:08

1 resposta

2

Com o GNU awk :

BEGIN { fname = "/dev/null" }
/<header>/,/<\/header>/ { hdr = hdr $0 "\n"; next }
/^<event / {
    events++
    if(events % 10000 == 1) {
        if(files++) close(fname)
        fname = sprintf("file%02d.txt", files)
        print hdr >fname
    }        
}            
{ print >>fname }

Para executá-lo: grave-o em um arquivo script.awk e execute:

gawk -f script.awk file.txt
    
por 02.10.2016 / 20:42