6

The situation is as follows:

I have a .txt file with results of several nslookups.

I want to loop tru the file and everytime it hits the string "Non-authoritative answer:" the scripts has to print the following 8 lines from that position. If it works I shoud get all the positive results in my screen :).

First I had the following code:

#!/bin/usr/python

file = open('/tmp/results_nslookup.txt', 'r')
f = file.readlines()

for positives in f:
        if 'Authoritative answers can be found from:' in positives:
                print positives
file.close()

But that only printed "Authoritative answers can be found from:" the times it was in the .txt.

The code what I have now:

#!/bin/usr/python

file = open('/tmp/results_nslookup.txt', 'r')
lines = file.readlines()

i = lines.index('Non-authoritative answer:\n')

for line in lines[i-0:i+9]:
        print line,

file.close()

But when I run it, it prints the first result nicely to my screen but does not print the other positve results.

p.s. I am aware of socket.gethostbyname("foobar.baz") but first I want to solve this basic problem.

Thank you in advance!

3
  • 2
    This is what grep was made for, in particular the -A option: grep -A8 'Non-authoritative answer:' /tmp/results_nslookup.txt Commented Oct 4, 2012 at 12:40
  • Or a simple awk one-liner: awk 'count-->0;/Non-authoritative answer:/{count=8;print}' /tmp/results_nslookup.txt Commented Oct 4, 2012 at 12:43
  • @Kevin I think you should have made that an answer. Of course, I have this rebellious streak for lateral answers... Commented Oct 4, 2012 at 14:21

2 Answers 2

7

You can use the file as an iterator, then print the next 8 lines every time you find your sentence:

with open('/tmp/results_nslookup.txt', 'r') as f:
    for line in f:
        if line == 'Non-authoritative answer:\n':
            for i in range(8):
                print(next(lines).strip())

Each time you use the next() function on the file object (or loop over it in a for loop), it'll return the next line in that file, until you've read the last line.

Instead of the range(8) for loop, I'd actually use itertools.islice:

from itertools import islice

with open('/tmp/results_nslookup.txt', 'r') as f:
    for line in f:
        if line == 'Non-authoritative answer:\n':
            print(''.join(islice(f, 8)))
Sign up to request clarification or add additional context in comments.

2 Comments

how to print the line which has "Non-authoriative answer" as well?
@Shashi just add print(line) as the first statement inside the if line == test.
4
file = open('/tmp/results_nslookup.txt', 'r')
for line in file:
    if line=='Non-authoritative answer:\n':
        for _ in range(8):
            print file.next()

By the way: don't ever use the name file for a variable because it is the name of a built-in function.

2 Comments

how to print the line which has "Non-authoriative answer" as well?
@Shasi: just put "print(line)" before the for loop. (But that will always print that fixed text.) You may also want to print (file.next())[:-1] instead, e.g. strip the newline to avoid printing double newlines

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.