Sponsored Content
Top Forums Shell Programming and Scripting loop with a counter on a constant in bash Post 302546718 by rxc23816 on Friday 12th of August 2011 02:18:04 AM
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
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.
 

7 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

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

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

6. 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

7. 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
GMT_SHELL_FUNCTIONS.SH(1gmt)				       Generic Mapping Tools				      GMT_SHELL_FUNCTIONS.SH(1gmt)

NAME
gmt_shell_functions.sh - Practical functions to be used in GMT bourne shell scripts SYNOPSIS
gmt_init_tmpdir gmt_remove_tmpdir gmt_clean_up [prefix] gmt_message message gmt_abort message gmt_nrecords file(s) gmt_nfields string gmt_get_field string gmt_get_region file(s) [options] gmt_get_gridregion file [options] gmt_get_map_width -R -J gmt_get_map_height -R -J gmt_set_psfile file gmt_set_framename prefix framenumber gmt_set_framenext framenumber DESCRIPTION
gmt_shell_functions.sh provides a set of functions to Bourne (again) shell scripts in support of GMT. The calling shell script should include the following line, before the functions can be used: . gmt_shell_functions.sh Once included in a shell script, gmt_shell_functions.sh allows GMT users to do some scripting more easily than otherwise. The functions made available are: gmt_init_tmpdir Creates a temporary directory in /tmp or (when defined) in the directory specified by the environment variable TMPDIR. The name of the temporary directory is returned as environment variable GMT_TMPDIR. This function also causes GMT to run in `isolation mode', i.e. all temporary files will be created in GMT_TMPDIR and the .gmtdefaults file will not be adjusted. gmt_remove_tmpdir Removes the temporary directory and unsets the GMT_TMPDIR environment variable. gmt_cleanup Remove all files and directories in which the current process number is part of the file name. If the optional prefix is given then we also delete all files and directories that begins with the given prefix. gmt_message Send a message to standard error. gmt_abort Send a message to standard error and exit the shell. gmt_nrecords Returns the total number of lines in file(s) gmt_nfields Returns the number of fields or words in string gmt_get_field Returns the given field in a string. Must pass string between double quotes to preserve it as one item. gmt_get_region Returns the region in the form w/e/s/n based on the data in table file(s). Optionally add -Idx/dy to round off the answer. gmt_get_gridregion Returns the region in the form w/e/s/n based on the header of a grid file. Optionally add -Idx/dy to round off the answer. gmt_map_width Expects the user to give the desired -R -J settings and returns the map width in the current measurement unit. gmt_map_height Expects the user to give the desired -R -J settings and returns the map height in the current measurement unit. gmt_set_psfile Create the output PostScript file name based on the base name of a given file (usually the script name $0). gmt_set_framename Returns a lexically ordered filename stem (i.e., no extension) given the file prefix and the current frame number, using a width of 6 for the integer including leading zeros. Useful when creating animations and lexically sorted filenames are required. gmt_set_framenext Accepts the current frame integer counter and returns the next integer counter. NOTES
1. These functions only work in the bourne shell (sh) and their derivatives (like ash, bash, ksh and zsh). These functions do not work in the C shell (csh) or their derivatives (like tcsh), and cannot be used in DOS batch scripts either. 2. gmt_shell_functions.sh were first introduced in GMT version 4.2.2 and have since been regularly expanded with other practical scripting short-cuts. If you want to suggest other functions, please do so by mailing to the GMT mailing list: gmt-help@lists.hawaii.edu. SEE ALSO
GMT(1), sh(1), bash(1), minmax(1), grdinfo(1) GMT 4.5.7 15 Jul 2011 GMT_SHELL_FUNCTIONS.SH(1gmt)
All times are GMT -4. The time now is 08:45 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy