Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check number of lines after a condition is met

Status
Not open for further replies.

crookedm

Technical User
Aug 18, 2012
6
GB
Hi all,

I've been away from scripting for a while, i've hit a brick wall with a script i'm writing!

The data file has the following:

zone: nameserver_absvc01a
10:00:00:00:00:00:00:00
10:00:00:00:00:00:00:00
10:00:00:00:00:00:00:00
10:00:00:00:00:00:00:00
10:00:00:00:00:00:00:00
10:00:00:00:00:00:00:00
10:00:00:00:00:00:00:00
10:01:00:00:00:00:00:00
10:00:00:00:00:00:00:00
10:00:00:00:00:00:00:00
zone: nameserver_absvc01a
10:00:00:00:00:00:00:00
10:00:00:00:00:00:00:00
10:00:00:00:00:00:00:00
10:00:00:00:00:00:00:00
10:00:00:00:00:00:00:00
10:00:00:00:00:00:00:00
10:00:00:00:00:00:00:00
10:01:00:00:00:00:00:00
10:00:00:00:00:00:00:00
10:00:00:00:00:00:00:00
etc etc

I need to check the first line contains svc then once thats found it needs to make sure there are 10 lines before the next zone. So far i've got, which is a start but man am I rusty !!!

finderror ()
{
for zone in logfile
do
ZONENAME=$(cat test.log|grep zone:)
echo "$ZONENAME" |awk '{print $2}'
done
}

 
Hi

[ul]
[li]for zone in logfile - What you expect from this line ? Looks pointless.[/li]
[li]cat test.log|grep zone: - Avoid the UUOC. [tt]grep[/tt] is able to read files, no need to spoon feed it with [tt]cat[/tt].[/li]
[li]echo "$ZONENAME" |awk '{print $2}' - Why extract $2 ? The description of the task not explains this.[/li]
[/ul]

If you want to use [tt]grep[/tt], ask it to output line numbers, then do the calculations :
Code:
grep -n [green][i]'zone:'[/i][/green] [b]test[/b][teal].[/teal]log [teal]|[/teal] [b]while[/b] [navy]IFS[/navy][teal]=[/teal][green][i]':'[/i][/green] [b]read[/b] nr blah[teal];[/teal] [b]do[/b] [teal](([/teal] p [teal]))[/teal] [teal]&&[/teal] echo [green][i]"lines between zones : $(( nr-p-1 ))"[/i][/green][teal];[/teal] [navy]p[/navy][teal]=[/teal][navy]$nr[/navy][teal];[/teal] [b]done[/b]
Tested with [tt]bash[/tt] and [tt]mksh[/tt].

But I would prefer Awk :
Code:
awk '[fuchsia]/zone:/[/fuchsia][teal]{[/teal][b]if[/b][teal]([/teal]p[teal])[/teal][COLOR=chocolate]print[/color][green][i]"lines between zones :"[/i][/green][teal],[/teal][blue]NR[/blue][teal]-[/teal]p[purple]-1[/purple][teal];[/teal]p[teal]=[/teal][blue]NR[/blue][teal]}[/teal]' test.log
Tested with [tt]gawk[/tt] and [tt]mawk[/tt].


Feherke.
[link feherke.github.com/][/url]
 
Hi, as I said i'm rusty so the for zone in logfile was and is pointless it was just me trying to get back into the scripting that I used to do, i.e. it was one of the very few things I remembered how to do. I should add that i've not scripted for over 4 years.

What is UUOC ?

The $2 was extracted as the string was read as one line starting at zone: so to extract the zone name I had to print $2

Thanks for the commands above I'm about to test them, apologies for coming across as a total gimp!

 
Hi just tested the commands and they work great, i'll get my notes out and put this command into my script then work on the rest. Gives me a great starting block. Many thanks.
 
Hi

crookedm said:
What is UUOC ?
The Useless use of [tt]cat[/tt] is a waste of resources :
Code:
[gray]# 2 processes, 1 pipe[/gray]
cat test.log[teal]|[/teal]grep zone:

[gray]# 1 process, 0 pipes[/gray]
grep zone: test.log
If you prefer to keep the order as in the pipeline, having the input file first, then the commands to process it :
Code:
[gray]# 1 process, 0 pipes[/gray]
[teal]<[/teal] test.log grep zone:


Feherke.
[link feherke.github.com/][/url]
 
Blimey, that's wonderful, i'm digging out some old scripts and all of them use cat! I could really cut down on processes used and really streamline some of them. Thanks again. Really appreciate it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top