loop with a counter on a constant in bash

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting loop with a counter on a constant in bash
# 1  
Old 08-12-2011
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
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx FINISH xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

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

The output may look like this

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.
# 2  
Old 08-26-2011
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

While loop with limit counter

#!/usr/bin/ksh c=0 while ]; do echo /tmp/unex NOT found, iter : $c; ((c = $c + 1)); sleep 2; done so, the above counter doesn't work, already tried both -lt & -gt, and changed || to && so what am I missing? Thanks in advance (5 Replies)
Discussion started by: unexistance
5 Replies

2. Shell Programming and Scripting

Loop counter resets by itself

Hi! Can anyone explain this? The counter CDR_count should go on forever, but it suddenly resets at every step of the FOR loop (I know this because $_file has exactly 378 records). However, the counter reset is OUTSIDE the FOR loop. What's going on?? #!/bin/bash if || ; then echo ... (8 Replies)
Discussion started by: Flavius
8 Replies

3. Shell Programming and Scripting

Loop usage with counter

Hi friends, I just want to try some thing with shell using loop and counter I have 30 Directory, in each directory number of files say 5, 10, 20 etc... directory_1 directory_2 directory_n what I want to do is read files from directory_1 say 5 files if my counter is like this m=2000 ... (6 Replies)
Discussion started by: Akshay Hegde
6 Replies

4. Shell Programming and Scripting

How to implement the counter in loop?

Hi, I am working on a script where I need to add one functionality i.e. to could the number of tar files at particular location...but the script is working in below way. 1) create sandbox 2) Drop old member function 3) addmember function 4) Apply checkpoint lable 5) Resync operation(This... (1 Reply)
Discussion started by: anuragpgtgerman
1 Replies

5. Shell Programming and Scripting

S# in a for loop - concatenate $(loop counter)

Hi, hope I am posting in the right section. My problem is that I have 2 or more arguments passed and I want to check if the arguments passed exists or not. The first argument should not exist and the remaining others should exist. example: ./shells.sh argument1 argument2 argument3 ... (5 Replies)
Discussion started by: fight4love
5 Replies

6. Shell Programming and Scripting

Constant update echo in BASH

Hi all, Basically Im trying to put the current time in a script in BASH. Tried the watch command, but its not really what I want. I will have lots of things in this script, current date and time being just a few). Any ideas? (4 Replies)
Discussion started by: mikejreading
4 Replies

7. Shell Programming and Scripting

Loop counter variable display

Hello everyone, how can I send output to the screen from a running script or tcl, in such a way that if a loop is executing I will see the rolling counter on my screen as the records are processed in the loop. I do not want the screen to scroll, though. In other words can a var's value be painted... (2 Replies)
Discussion started by: lifespan
2 Replies
Login or Register to Ask a Question