title: Bash: Inserting one file's content into another file after the pattern
body: <p>I'm trying to write a bash script, which will do the following:</p>
  
  <ol>
  <li>reads the content from the first file (as a first argument)</li>
  <li>reads the content from the second file (as a second argument)</li>
  <li>finds the line in the second file with the given pattern (as a third argument)</li>
  <li>inserts text from the first file to the second file after the line of the pattern.</li>
  <li>prints final file on the screen.</li>
  </ol>
  
  <p>For example:</p>
  
  <p>first_file.txt:</p>
  
  <pre><code>111111
  1111
  11
  1
  </code></pre>
  
  <p>second_file.txt:</p>
  
  <pre><code>122221
  2222
  22
  2
  </code></pre>
  
  <p>pattern:</p>
  
  <pre><code>2222
  </code></pre>
  
  <p>output:</p>
  
  <pre><code>122221
  111111
  1111
  11
  1
  2222
  111111
  1111
  11
  1
  22
  2
  </code></pre>
  
  <p>What should I use to realize this functionality on BASH?</p>
  
  <p>I wrote the code, but it doesn't work properly (why?):</p>
  
  <pre><code>    #!/bin/bash
  
      first_filename="$1"
      second_filename="$2"
      pattern="$3"
  
      while read -r line
      do
      if [[ $line=˜$pattern ]]; then
              while read -r line2
              do
                      echo $line2
              done &lt; $second_filename
      fi
      echo $line
      done &lt; $first_filename
  </code></pre>