Writing lines from a file
<p>In this sample, I find a string in a file and write the matching line to another file. That is basic and what I can do at the moment. </p>

<pre><code>fd_w = open('tt', 'w')
with open('r.0', 'r') as IRR:
    for line in IRR:
        if 'status: ASSIGNED PA' in line:
            towrite = "%s" % (line)
fd_w.write(towrite)   
fd_w.close()
</code></pre>

<p>What I really want to do is, when there is a matching line, to get the lines prior to the matching line until there is a space and similarly get the lines after the matching line until there is a space.</p>

<p>This is a sample from the file that is being read.</p>

<pre><code>admin-c:        DUMY-RIPE
tech-c:         DUMY-RIPE
status:         ASSIGNED PA
mnt-by:         AS6667-MNT

admin-c:        DUMY-RIPE
tech-c:         DUMY-RIPE
status:         ASSIGNED PA
mnt-by:         DATANET-NOC
</code></pre>

<p>I have been trying to sort out how to define two functions, like get previous lines and next lines after matching line. So any idea, any help is highly appropriated.</p>
