title: How to insert a new line of code in only perl scripts with Bash script
body: <p>I have a Linux folder tree with mixed shell, Perl and Python scripts. None of the scripts have consistent file extensions (.pl, .perl, .py, .sh, or no extension at all). I need to identify which files are Perl scripts, then add a new line of code to set a variable if the variable is not already in the Perl script.</p>
  
  <p>Thanks to <a href="https://stackoverflow.com/questions/9473060/how-to-insert-newline-character-after-comma-in-with-sed">How to insert newline character after comma in `),(` with sed?</a> I came up with this code that uses sed to insert the new line of code.</p>
  
  <p>This code works, but is there a more efficient way to do it?</p>
  
  <pre><code>#! /bin/bash
  
  NL='
  $|++'
  
  for f in `find "$1" -type f`
  do
      if [ $(grep -cP "^\#\!/.+/perl" "${f}") -gt 0 ]
      then
          if [ $(grep -c "$|" "${f}") -eq 0 ]
          then
              sed -i -r "s/^#!\/(.+)\/perl(.+)$/#!\/\1\/perl\2\\${NL}/g" "${f}"
          fi
      fi
  done
  </code></pre>