title: How to find and replace X lines in one text file with Y lines in another text file?
body: <p>I asked this question: <a href="https://stackoverflow.com/questions/21637282/how-to-find-and-replace-multiple-lines-in-text-file/21637591?noredirect=1#21637591">How to find and replace multiple lines in text file?</a>
  but was ultimately unclear in my question so I'm asking another one to be more specific.</p>
  
  <p>I have Python 2.7.</p>
  
  <p>I have three text files, <code>data.txt</code>, <code>find.txt</code> and <code>replace.txt</code>. </p>
  
  <p><code>data.txt</code> is about 1MB large file with several thousand lines.  Now, I have a <code>find.txt</code> file containing X number of lines that I want to find in <code>data.txt</code> and replace with Y number of lines in <code>replace.txt</code>  X and Y may be the same number, or it may not.  </p>
  
  <p>For example: </p>
  
  <p><strong>data.txt</strong></p>
  
  <pre><code>pumpkin
  apple
  banana
  cherry
  himalaya
  skeleton
  apple
  banana
  cherry
  watermelon
  fruit
  </code></pre>
  
  <p><strong>find.txt</strong></p>
  
  <pre><code>apple
  banana
  cherry
  </code></pre>
  
  <p><strong>replace.txt</strong></p>
  
  <pre><code>1
  2
  3
  4
  5
  </code></pre>
  
  <p>So, in the above example, I want to search for all occurences of <code>apple</code>, <code>banana</code>, and <code>cherry</code> in the data and insert <code>1,2,3,4,5</code> in its place.</p>
  
  <p>So, the resulting <code>data.txt</code> would look like: </p>
  
  <pre><code>pumpkin
  1
  2
  3
  4
  5
  himalaya
  skeleton
  1
  2
  3
  4
  5
  watermelon
  fruit
  </code></pre>
  
  <p>Or, if the number of lines in <code>replace.txt</code> were less than that of <code>find.txt</code>: </p>
  
  <pre><code>pumpkin
  1
  2
  himalaya
  skeleton
  1
  2
  watermelon
  fruit
  </code></pre>
  
  <p>I am having some trouble with the right approach to this as my <code>data.txt</code> is about 1MB so I want to be as efficient as possible.  One dumb way is to concatenate everything into one long string and use <code>replace</code>, and then output to a new text file so all the line breaks will be restored.</p>
  
  <pre><code>data = open("data.txt", 'r')
  find = open("find.txt", 'r')
  replace = open("replace.txt", 'r')
  
  data_str = ""
  find_str = ""
  replace_str = "" 
  
  for line in data: # concatenate it into one long string
      data_str += line
  
  for line in find: # concatenate it into one long string
      find_str += line
  
  for line in replace: 
      replace_str += line
  
  
  new_data = data_str.replace(find, replace)
  new_file = open("new_data.txt", "w")
  new_file.write(new_data)
  </code></pre>
  
  <p>But this seems so convoluted and inefficient for a large data file like mine.</p>
  
  <p>The pseudo-code for something that I would like to see: </p>
  
  <p>Something like this: </p>
  
  <pre><code>(x,y) = find_lines(data.txt, find.txt) # returns line numbers in data.txt that contains find.txt
  replace_data_between(x, y, data.txt, replace.txt) # replaces the data between lines x and y with replace.txt
  
  def find_lines(...):
      location = 0
  
      LOOP1: 
      for find_line in find:
          for i, data_line in enumerate(data).startingAtLine(location):
              if find_line == data_line:
                  location = i # found possibility
  
      for idx in range(NUMBER_LINES_IN_FIND):
          if find_line[idx] != data_line[idx+location]  # compare line by line
              #if the subsequent lines don't match, then go back and search again
              goto LOOP1
  </code></pre>
  
  <p>As you can see, I am having trouble with the logic of this all.  Can someone point me in the right direction?</p>