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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find Line, Move Down 3 lines Help

Status
Not open for further replies.

Trancemission

Technical User
Oct 16, 2001
108
GB
I have 100's of files which contain whois output. I need to search these files for a string, move down 3 lines then print the next 2/3 lines (well pipe them to grep)

The only problem is I can't do it :) Any help would be appreciated.

File format (db.domain.com):

Blah Blah Blah.....
Blah Blah Blah.....
Blah Blah Blah.....
Blah Blah Blah.....
Domain servers listed in order: <- Text I want to search for


ns.domainserver.com <- Text I want to retrieve
ns2.domainserver.com <- Text I want to retrieve

Blah Blah Blah......
Blah Blah Blah......
Blah Blah Blah......
Blah Blah Blah......
Blah Blah Blah......
Blah Blah Blah...... Trancemission
=============
If it's logical, it'll work!
 
I don't know what you man by &quot;2/3 lines&quot;. Is that &quot;2 or 3 lines&quot; and what criteria do you use to decide whether to print 2 or 3? But here's something to get you started.
Code:
/^Domain servers listed in order:/ { knt=1}
knt{knt++
    if (knt==5 || knt==6) print
    else if (knt==7) knt=0
   }
Hope this helps. CaKiwi
 
Thats great!

Nice and easy, simple to understand yet powerful just my type :)

FYI:

Some of the whois lines contain 2 entries, some have 3.

I want to always print the lines.

Many Thanks

:)
 
Sorry to come back to the post but only just had chance [attempt] to write this script with the new info. I am experiancing some frustrating problems:

The file I am searching looks like:

<start med>

Domain Name: DOMAIN.CO.UK

Registered For: A Company
Domain Registered By: SOMEHOST

Registered on 19-Jun-1997.

Record last updated on 07-Jun-2001 by nominet@Nominet.

Domain servers listed in order:

NS0.NAMESERVER.CO.UK 158.152.1.65
NS1.NAMESERVER.CO.UK 158.152.1.193

WHOIS database last updated at 04:02:00 16-May-2002

The NIC.UK Registration Host contains information ONLY for

<stop med>

The lines that I am looking to get out are:

NS0.NAMESERVER.CO.UK 158.152.1.65
NS1.NAMESERVER.CO.UK 158.152.1.193

I have around 900 of these files and nearly all the NAMSERVER lines are different.

At the mo my script is simply:

/^Domain servers listed in order/ { knt=1}
{knt++
if (knt==4) print
else if (knt==5) knt=0
}


Which produces:

Registered For: A Company
Record last updated on 07-Jun-2001 by nominet@Nominet.
NS1.NAMESERVER.CO.UK 158.152.1.193

I have tried chaging the if knt== line with diferent numbers but still get [to me] random lines.


Help appreciated :)

Many Thanks Trancemission
=============
If it's logical, it'll work!
 
Hi !

You forget to put the knt at start of the line {knt++

The script you use print one line out of four (the 4th, 8th, 12th and so on).

What you want is the count up and print to be done only after seeing the line Domain servers listed ... so only when the knt variable is not zero.

Here is a more explicit ('who said verbose ?') version of the same script:

Code:
#!/usr/bin/awk

# Commands to be done before reading the first line
BEGIN {
    # Initialization not needed, just to be on the safe side
    inDomainServerList = 0;
    foundNonEmptyLine = 0;
}

# Look for a non empty line (with a least one char that is
# not a space or a tab) and only if we are in the domain 
# server list (see last pattern below).

/[^ \t]/ && (inDomainServerList == 1) {
    # Ok we found a non empty line: it is a domaine/ip line
    foundNonEmptyLine = 1;
    # print it
    print;
}

# If we found an empty line (with only space or tabs)
# after finding a non empty one:
# we just quit the domain server list.

/^[ \t]*$/ && (foundNonEmptyLine == 1) {
    # Reset the variable
    inDomainServerList = 0;
}

# Last, check if we just entered the domain server list

/Domain servers listed in order:/ {
    # Ok we are in the domain server list
    inDomainServerList = 1;
    # we just have to skip the following empty lines
    foundNonEmptyLine = 0;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top