title: Bash: Insert line into source code file after initial comments
body: <p>I need to use bash to insert a line into a python file. This line needs to appear after any initial comments in the the file.</p>
  
  <p>So given the file:</p>
  
  <pre><code>#!/usr/bin/python
  # This is just 
  # an example comment
  
  moo = "cow"
  ... etc ...
  </code></pre>
  
  <p>I need a bash command to insert a new line like this:</p>
  
  <pre><code>#!/usr/bin/python
  # This is just 
  # an example comment
  NEW LINE GOES HERE
  
  moo = "cow"
  ... etc ...
  </code></pre>
  
  <p>I am entirely stumped on how to do this. I have tried looping over the file line by line, but that just ends up being pretty horrific and severely messing up the file's whitespace.</p>
  
  <p>Any suggestions would be great!</p>
  
  <p>Adam</p>
  
  <p>PS. Yes, this is a bit of a weird thing to do, it is for part of a continuous integration build script.</p>
  
  <hr>
  
  <p><strong>Edit</strong></p>
  
  <p>For the record, the code I was trying was:</p>
  
  <pre><code>insert_setup_code() {
      installed=false
      tmpfile="/tmp/$RANDOM"
  
      cat "$INSTALL_TO" | while read -d \n l; do
          echo "$l" &gt;&gt; $tmpfile
          if [[ ! $installed &amp;&amp; ! `echo "$l" | grep "^#"` ]]; then
              echo "LINE OF CODE HERE" &gt;&gt; $tmpfile
              installed=true
          fi
      done
  }
  </code></pre>