title: Add new line to a file after matching pattern - Python
body: <p>I have a file called 'file.txt' and its contents are as below.</p>
  
  <pre><code> [Jack]
   sv0f3fj3jff0j
  
  
   [Tom]
   343767y6y6y5yu
  </code></pre>
  
  <p>I have to add new line just after each names(by reading the user name as input). Can any one please help me ? I have tried using the below steps but didn't succeeded.</p>
  
  <pre><code>#!/usr/bin/python36
  
  inv_file = '/root/file.txt'
  cn_search = input("\nEnter the name: ")
  new_line = input("\nEnter the new line : ")
  with open(inv_file) as in_file:
       buf = in_file.readlines()
       print(buf.replace('[').replace(']'))
  with open(inv_file, "w") as in_file:
       for line in buf:
           if line.startswith('[') and line.endswith(']'):
              mod_line = line.replace('[', '').replace(']', '')
              if mod_line == cn_search:    
                 buf  = buf + "\n" + new_ip_ex
                 out_file.write(buf)
  </code></pre>