adding number based on pattern using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting adding number based on pattern using awk
# 1  
Old 07-08-2011
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 ?

HTML Code:
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
# 2  
Old 07-08-2011
Try and adaptthe following script (rag.ksh) :
Code:
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) :
Code:
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 :
Code:
$ 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.
# 3  
Old 07-08-2011
Thank you very much perrie Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to update value based on pattern match in another file

In the awk, thanks you @RavinderSingh13, for the help in below, hopefully it is close as I am trying to update the value in $12 of the tab-delimeted file2 with the matching value in $1 of the space delimeted file1. I have added comments for each line as well. Thank you :). awk awk '$12 ==... (10 Replies)
Discussion started by: cmccabe
10 Replies

2. Shell Programming and Scripting

Number of matches and matched pattern(s) in awk

input: !@#$%2QW5QWERTAB$%^&* The string above is not separated (or FS=""). For clarity sake one could re-write the string by including a "|" as FS as follow: !|@|#|$|%|2QW|5QWERT|A|B|$|%|^|&|* Here, I am only interested in patterns (their numbers are variable between records) containing... (16 Replies)
Discussion started by: beca123456
16 Replies

3. Shell Programming and Scripting

How to split a file based on pattern line number?

Hi i have requirement like below M <form_name> sdasadasdMklkM D ...... D ..... M form_name> sdasadasdMklkM D ...... D ..... D ...... D ..... M form_name> sdasadasdMklkM D ...... M form_name> sdasadasdMklkM i want split file based on line number by finding... (10 Replies)
Discussion started by: bhaskar v
10 Replies

4. Shell Programming and Scripting

How to add columns based on a pattern using awk?

Hi, I have a file with more than 1000 lines with ~14 columns. I need to find all the lines with matching value in column 14 and then add column 6 in all the lines before printing them out.. e.g if this is the input file: abc test input 10 for process 2345 abc test input 15 for process 2348... (1 Reply)
Discussion started by: xkdasari
1 Replies

5. Shell Programming and Scripting

adding a number with sed or awk.

Hi.. I have this delicate problem..:wall: I have this huge ldif file with entry's like this example below.. And I need to change the following entrys. telephoneNumber: emNotifNumber: billingnumber= BillingNumber: Al these entrys has a number like 012345678 and it needs to add one more... (15 Replies)
Discussion started by: pelama
15 Replies

6. Shell Programming and Scripting

print the whole row in awk based on matched pattern

Hi, I need some help on how to print the whole data for unmatched pattern. i have 2 different files that need to be checked and print out the unmatched patterns into a new file. My sample data as follows:- File1.txt Id Num Activity Class Type 309 1.1 ... (5 Replies)
Discussion started by: redse171
5 Replies

7. Shell Programming and Scripting

sed/awk : how to delete lines based on IP pattern ?

Hi, I would like to delete lines in /etc/hosts on few workstations, basically I want to delete all the lines for a list of machines like this : for HOST in $(cat stations.lst |uniq) do # echo -n "$HOST" if ping -c 1 $HOST > /dev/null 2>&1 then HOSTNAME_val=`rsh $HOST "sed... (3 Replies)
Discussion started by: albator1932
3 Replies

8. Shell Programming and Scripting

Sed or awk : pattern selection based on special characters

Hello All, I am here again scratching my head on pattern selection with special characters. I have a large file having around 200 entries and i have to select a single line based on a pattern. I am able to do that: Code: cat mytest.txt | awk -F: '/myregex/ { print $2}' ... (6 Replies)
Discussion started by: usha rao
6 Replies

9. UNIX for Dummies Questions & Answers

Adding a column with the row number using awk

Is there anyway to use awk to add a first column to my data that automatically goes from 1 to n , where n is the numbers of my rows?:confused: (4 Replies)
Discussion started by: cosmologist
4 Replies

10. Shell Programming and Scripting

Split File Based on Line Number Pattern

Hello all. Sorry, I know this question is similar to many others, but I just can seem to put together exactly what I need. My file is tab delimitted and contains approximately 1 million rows. I would like to send lines 1,4,& 7 to a file. Lines 2, 5, & 8 to a second file. Lines 3, 6, & 9 to... (11 Replies)
Discussion started by: shankster
11 Replies
Login or Register to Ask a Question