INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Check number of lines after a condition is met

Check number of lines after a condition is met

(OP)
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
}

RE: Check number of lines after a condition is met

Hi

  • for zone in logfile - What you expect from this line ? Looks pointless.
  • cat test.log|grep zone: - Avoid the UUOC. grep is able to read files, no need to spoon feed it with cat.
  • echo "$ZONENAME" |awk '{print $2}' - Why extract $2 ? The description of the task not explains this.
If you want to use grep, ask it to output line numbers, then do the calculations :

CODE --> command-line

grep -n 'zone:' test.log | while IFS=':' read nr blah; do (( p )) && echo "lines between zones : $(( nr-p-1 ))"; p=$nr; done 
Tested with bash and mksh.

But I would prefer Awk :

CODE --> command-line

awk '/zone:/{if(p)print"lines between zones :",NR-p-1;p=NR}' test.log 
Tested with gawk and mawk.

Feherke.
http://feherke.github.com/

RE: Check number of lines after a condition is met

(OP)
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!

RE: Check number of lines after a condition is met

(OP)
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.

RE: Check number of lines after a condition is met

Hi

Quote (crookedm)

What is UUOC ?
The Useless use of cat is a waste of resources :

CODE

# 2 processes, 1 pipe
cat test.log|grep zone:

# 1 process, 0 pipes
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

# 1 process, 0 pipes
< test.log grep zone: 

Feherke.
http://feherke.github.com/

RE: Check number of lines after a condition is met

(OP)
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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close