title: Replace line in text file with line from other text file
body: <p>My question is a variant of the following one: </p>
  
  <p><a href="https://stackoverflow.com/questions/11145270/bash-replace-an-entire-line-in-a-text-file">bash: replace an entire line in a text file</a></p>
  
  <p>The problem there was to replace the Nth line of a file with a given string (a replacement line). In my case, I can't just type the replacement line, but I have to read it from another file. </p>
  
  <p>For example: </p>
  
  <p>textfile1: </p>
  
  <pre><code>my line
  your line
  his line
  her line
  </code></pre>
  
  <p>textfile2: </p>
  
  <pre><code>our line
  </code></pre>
  
  <p>I want to replace the 2nd line of textfile1 with the line from textfile2. </p>
  
  <p>I thought I could just read the textfile2 </p>
  
  <pre><code>while IFS= read SingleLine 
  </code></pre>
  
  <p>etc. and then use <code>$SingleLine</code> as the replacement line, but I failed... Depending on the type of quotes I used (please excuse my ignorance...) I ended up replacing the line in question with the text <strong>$SingleLine</strong> or with <strong>SingleLine</strong> or just getting an error message :-[ </p>
  
  <p>I am sure you can help me!!</p>
  
  <p>EDIT about the solution: 
  I went for the inline solution with the small change </p>
  
  <pre><code>sed '2d;1r textfile2' textfile1 &gt; newfile1 
  </code></pre>
  
  <p>To replace the Nth line, the solution would be (see comments on accepted solution for explanations)</p>
  
  <pre><code>sed 'Nd;Mr textfile2' textfile1 &gt; newfile1 
  </code></pre>
  
  <p>with N the desired line number and M=N-1. </p>
  
  <p>Thank you all!</p>