title: UNIX; merge two files with common words
body: <p>I have two files; file1.txt and file2.txt. And want to merge them like:</p>
  
  <pre><code>name1 note1
  name2 note2
  name3 note3
  </code></pre>
  
  <p>file1.txt</p>
  
  <pre><code>name2 note2_2
  name3 note2_3
  name1 note2_1
  </code></pre>
  
  <p>file2.txt</p>
  
  <pre><code>name1 note1 note2_1
  name2 note2 note2_2
  name3 note3 note2_3
  </code></pre>
  
  <p>result.txt</p>
  
  <p>I did some code but I'm not sure how to append it.</p>
  
  <pre><code>#!/bin/bash
  
  cut -d' ' -f1 file1.txt &gt; names1.txt
  
  while read -a rows
  do
      for i in "${rows[@]}"
      do
      grep "$i" | cut -f 2- -d ' ' file2.txt &gt; new.txt
      done
      *here I should append it (I think)*
  done &lt; names1.txt
  </code></pre>
  
  <p>I know that sed can be use for appending like; <code>sed 's/$/ *the first word of each line from new.txt*/' file1.txt</code>
  Or using <code>paste file1.txt new.txt &gt; fileresults.txt</code></p>