# adding number based on pattern using awk

**URL:** <https://community.unix.com/t/adding-number-based-on-pattern-using-awk/291176>\
**Category:** Shell Programming and Scripting\
**Created:** [July 8, 2011, 6:22am UTC](https://community.unix.com/t/adding-number-based-on-pattern-using-awk/291176 "2011-07-08T06:22:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![raghavendra.nsn](https://community.unix.com/letter_avatar/raghavendra.nsn/32/6_449c533f586939b6e5f246632a46421f.png) [@raghavendra.nsn](https://community.unix.com/u/raghavendra.nsn)\
**Post date:** [July 8, 2011, 6:22am UTC](https://community.unix.com/t/adding-number-based-on-pattern-using-awk/291176/1 "2011-07-08T06:22:06Z")

</div>

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 ?

```auto
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

```

---

<div class="post-metadata">

**Author:** ![aigles](https://community.unix.com/user_avatar/community.unix.com/aigles/32/118_2.png) [@aigles](https://community.unix.com/u/aigles)\
**Post date:** [July 8, 2011, 11:21am UTC](https://community.unix.com/t/adding-number-based-on-pattern-using-awk/291176/2 "2011-07-08T11:21:05Z")

</div>

Try and adaptthe following script (rag.ksh) :

```nohighlight
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) :

```nohighlight
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 :

```nohighlight
$ 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.

---

<div class="post-metadata">

**Author:** ![raghavendra.nsn](https://community.unix.com/letter_avatar/raghavendra.nsn/32/6_449c533f586939b6e5f246632a46421f.png) [@raghavendra.nsn](https://community.unix.com/u/raghavendra.nsn)\
**Post date:** [July 8, 2011, 11:45am UTC](https://community.unix.com/t/adding-number-based-on-pattern-using-awk/291176/3 "2011-07-08T11:45:26Z")

</div>

Thank you very much perrie 🙂
