loop with a counter on a constant in bash

Hello Everyone,

I'm in need of assistance on creating a script with a counter on a certain string.

Basically this script opens a log file and displays certain log data. There are two key words in the log. START and FINISH. In between the START and FINISH is a variable ACTNUMBER. It will look like ACTNUMBER==1234 The number 1234 will vary.

So I have a variable ACTNUMBER==$ACTNUMBER

The two constants are START and FINISH
The ACTNUMBER will come between a START and FINISH.
Sometimes there are two START and two FINISH. and the ACTNUMBER will fall between those. Usually its about 3 - 4 lines of data in the log.

My ouput should look like this

START xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ACTNUMBER==1234
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx FINISH xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

There are some instances in which there is a nested START and FINISH

The output may look like this

STARTxxxxxxxxxxxxxxxSTARTxxxxxxxxxxACTNUMBER==1234xxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxFINISHxxxxxxxx
xxxxxxxxxxxxx FINISH xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

The x's are random log information that I need as well.

I was not able to accomplish this with grep as grep does one line at a time.

I tried with nawk using this syntax
nawk -vact="$ACTNUMBER" '$0~"START.*ACTNUMBER="act,/FINISH/' logfil

This commmand did not give me all the information I needed. It left some lines of data out, like the second line after the START. It also did not give me the second FINISH in the case of a nested START and FINISH. So it would give me a STARTxxxxxSTARTxxxxACTNUMBER==1234 xxxxx FINISH and not the second FINISH (this is four lines of data usually coming out of a log file)

I was told the only way I can do this is with a counter.

What is a good algorithm to use to increment the counter on START and decrement on FINISH?

Basically i need it to look for the line with START, find the ACT NUMBER. If it finds it then print that line. Go to next line (2nd line), even if it doesnt find START, print the line, go to next line (3rd line) look for a FINISH if it finds this line then stop, then carriage return.

In the case of a nested scenario with two START and two FINISH
Look for a START, look for another START.. Look for the ACTNUMBER. Print the line. Go to next line. Even if there is no START. Print the line. Go to third line. If there is no START or FINISH. Print the line. Go to next line. If it finds a FINISH, look for another FINISH, print line and stop - carraige return (space on next line of output)

So it needs to look for the START, ACTNUMBER and print all the lines until it finds a finish.

However I need a script with a counter for the nested scenario. I was told to use a counter for this specifcally and thats where I'm struggling.
Also needs to be a recursive loop. Can this be done with a counter and nawk?

This is on Solaris 10 Unix.

I have an idea of the proper syntax for a counter. I actually have one working that is doing a line count on the script. That works, but I'm having issues creating a counter to do an increment on START and a decrement on BEGIN and integrating this into the script to get me what i NEED.

I am super novice with unix. Any help would be appreciated.

I do not understand very well what influence the value of ACTNUMBER has, because
it seems to me that a START tag starts printing independently of the presence of ACTNUMBER.
In any case here is a code which prints all lines from the first START tag until (and including)
the corresponding FINISH tag on the same level, keeping track of the nested levels.

nawk -vlevel=0 '/START/{level++}/FINISH/{print; level--; next}{if (level>0) print}'

Possibly this is useful and you may extend it according to your specific needs.