adding number based on pattern using awk

Hi,

I want to add numbers based on pattern .

in the below ouput file , i want number to be added after pattern line ---- , ie 1 and next is also 1 and next is 2 and next is 3 after adding 4 numbers say output is 7 should be redirected to another file . like this it should add 4 digits after a pattern ------ and redirect it to file. the input file is always multiple of 4 count will be there.

any help with awk will be appreciated ?

COUNT(*)               
---------------------- 
1                

1 rows selected

COUNT(*)               
---------------------- 
1                

1 rows selected

COUNT(*)               
---------------------- 
2            

1 rows selected

COUNT(*)               
---------------------- 
3               

1 rows selected

COUNT(*)               
---------------------- 
2       

1 rows selected

COUNT(*)               
---------------------- 
4                

1 rows selected

COUNT(*)               
---------------------- 
1              

1 rows selected

COUNT(*)               
---------------------- 
2              

1 rows selected

Try and adaptthe following script (rag.ksh) :

awk -v Count=4 -v Outfile='rag_%d.out' '
    function printSum(   out) {
        if (SumCount) {
            out = sprintf(Outfile, ++FileCount);
            print SumValue > out;
            close(out);
            SumValue = SumCount = 0;
        }
    }
    /^----/ {
        getline;
        SumValue += $1;
        SumCount++;
        if (SumCount % Count == 0) printSum();
    }
    END {
        printSum();
    }
' rag.dat

Inputfile (rag.dat) :

COUNT(*)
----------------------
1

1 rows selected

COUNT(*)
----------------------
1

1 rows selected

COUNT(*)
----------------------
2

1 rows selected

COUNT(*)
----------------------
3

1 rows selected

COUNT(*)
----------------------
2

1 rows selected

COUNT(*)
----------------------
4

1 rows selected

COUNT(*)
----------------------
1

1 rows selected

COUNT(*)
----------------------
2

1 rows selected

Output :

$ ls rag*.out
rag*.out: No such file or directory
$ ./rag.ksh
$ ls rag*.out
rag_1.out  rag_2.out
$ head rag*.out
==> rag_1.out <==
7

==> rag_2.out <==
9
$

Jean-Pierre.

Thank you very much perrie :slight_smile: