title: Merge two bash history files
body: <p>Let's say I have two bash history files as follows:</p>
  
  <p><strong>history1.txt</strong>:</p>
  
  <pre><code>    1  ls
      2  cd foo
  ...
    921  history &gt; history1.txt
  </code></pre>
  
  <p><strong>history2.txt</strong>:</p>
  
  <pre><code>  154  vim /etc/nginx/nginx.conf
    155  service nginx restart
  ...
   1153  history &gt; history2.txt
  </code></pre>
  
  <p>I knew I could easily write a bash script to merge these two files together so that the resulting file contains line 1 to 1153 without duplicate history entries... like the following bash scripts:</p>
  
  <p><strong>merge.sh</strong></p>
  
  <pre><code>HEAD=`head -n 1 history2.txt | sed -e 's/^[[:space:]]*//'`
  sed -n -e "1,/$HEAD/p" history1.txt &gt; merged.txt
  sed -e "1,$ s/$HEAD//" -e '/^\s*$/d' history2.txt &gt;&gt; merged.txt
  </code></pre>
  
  <p>But I spent way more time than I'd like to admit trying to find a way to accomplish this using only unnamed pipes, sed, and/ or any other common Linux utils <strong>WITHOUT variables and command substitution (`command`)</strong>.  I didn't have any success :(</p>
  
  <p>Any Linux shell gurus or sed masters know if this is possible?</p>
  
  <p>NOTE:  I know the merge.sh script will not work for all edge cases.</p>