Python to insert a line at a random location of a large text file with condition
<p>I have a 30000 plus line text file that I need to modify using Python.  What I want to do is the following:</p>

<ol>
<li><p>There is a specific keyword1 in the file that has a certain device name following the keyword1.  I need to identify keyword1 in all lines and retrieve all device names, then store them in a list for later use</p>

<p>I have achieved this by using Regular Expression</p></li>
<li><p>Once I have the device names list, I need to insert a newline with "fixed format + device name" into the file at a series of specific locations following keyword2</p>

<p>This is where I run into problem.  </p>

<p>Originally I used the simple counting method.  I <code>pattern.search(line)</code> throughout the file and once the keyword1 is identified, I locate the inserting location by counting i + 5, where i is the index of the line where keyword1 is identified.  However, it turns out that the line order in the file does matter so I will need to insert the newline only after the line where keyword2 is located.  What's more troublesome is that keyword2 exists everywhere in the file.  It is only those keyword2 within 7 lines after keyword1 that needs to be considered.</p></li>
</ol>

<p>To give an example:</p>

<pre><code> This is a random line with KEYWORD2      &lt;--- this keyword2 is not considered
 This is a random line
 This is a random line
 This is a random line, KEYBOARD1 "DEVICE NAME"     &lt;--- id keyword1 and record DEVICE
 This is a random line
 This is a random line
 This is a random line
 This is a random line
 This is a random line with KEYWORD2      &lt;--- this keyword2 is considered
</code></pre>

<p>Any suggestion is appreciated.  Thanks in advance</p>
