title: cat, grep & awk - both while read line & while read file in 1 loop?
body: <hr>
  
  <p>Hi, </p>
  
  <p>Thanks to alot of searching on stackoverflow (great resource!) last couple of days I succeeded in this, and even succeeded in the following issue, that was the output resulted in doubling of the lines everytime I ran the command. Thanks to an awk command which was able to remove double lines.
  I'm pretty far in my search, but am missing 1 option.
  Using both MacosX and linux by the way.</p>
  
  <p>What I'm trying to do is parse through my notes <em>(all plain text .md files)</em>, searching for words/tags in a text file <em>(called greplist.txt)</em>, and parsing matched lines in separate text files with the same name as the searchword/tag <em>(eg @computer.md)</em>.</p>
  
  <p><strong>Selection of contents of <em>greplist.txt</em> are:</strong></p>
  
  <pre><code>@home
  @computer
  @Next
  @Waiting
  </code></pre>
  
  <p><strong>example contents of 2 .md files:</strong></p>
  
  <p><em>school.md:</em></p>
  
  <pre><code>* find lost schoolbooks @home
  * do homework @computer
  </code></pre>
  
  <p><em>fun.md</em></p>
  
  <pre><code>* play videogame @computer
  </code></pre>
  
  <p><strong>With this terminal command (that works great, but not perfect yet)</strong></p>
  
  <pre><code>$ cat greplist.txt | while read line; do grep -h "$line" *.md &gt;&gt; $line.md.tmp; mv $line.md.tmp $line.md; awk '!x[$0]++' &lt; $line.md &gt; $line.md.tmp &amp;&amp; mv $line.md.tmp $line.md ;done
  </code></pre>
  
  <p><strong>Results</strong></p>
  
  <p>The result for <em>@computer.md</em> :</p>
  
  <pre><code>* do homework @computer
  * play videogame @computer
  </code></pre>
  
  <p>And <em>@home.md</em> would look like this</p>
  
  <pre><code>* find lost schoolbooks @home
  </code></pre>
  
  <p>So far so great! Already really really happy with this. Especially since the added moving/renaming of the files, it is also for me possible to add extra tasks/lines to the @ tag .md files, and be included in the file without being overwritten the next time I run the command. Awesomecakes!</p>
  
  <p>Now the only thing I miss is that I wish that in the output of the @ tag .md files behind the task also the output also list the filename (without extensions) in between brackets behind the search result (so that nvalt can use this as an internal link)</p>
  
  <p><strong>So the desired output of example <em>@computer.md</em> would become:</strong></p>
  
  <pre><code>* do homework @computer [[school]]
  * play videogame @computer [[fun]]
  </code></pre>
  
  <p>I tried playing around with this with the -l and -H in the grep command instead of -h, but the output it just gets messy somehow. (Not even tried adding the bracket yet!)</p>
  
  <p>Another this I tried was this, but it doesn't do anything it seams. It does however illustrate probably what I'm trying to accomplish. </p>
  
  <pre><code>$ cat greplist.txt | while read line; do grep -h "$line" *.md | while read filename; do echo "$filename" &gt;&gt; $line.md.tmp; mv $line.md.tmp $line.md; awk '!x[$0]++' &lt; $line.md &gt; $line.md.tmp &amp;&amp; mv $line.md.tmp $line.md ;done
  </code></pre>
  
  <p>So the million Zimbabwean dollar question is: How to do this. I tried and tried, but this is above my skill level atm. Very eager to find out the solution!</p>
  
  <p>Thanks in advance.</p>
  
  <p>Daniel Dennis de Wit</p>