title: Live Linux grep script - only print positive results
body: <p>I try to code a bash 4 (or sh, if that's possible) script that does the following:
  1st parameter: the path to the compressed modules to search, could be "."
  2nd the search pattern to look for</p>
  
  <p>The script so far looks like this (be aware that it is not foolproof, it not checks if both parameters are valid, it not even checks if 2 parameters are given at all, all that I would add if I get the script to work as I want it to)</p>
  
  <pre><code>#!/bin/bash
  for module in $(find $1 -type f -name "*.xzm"); do
      echo $module
      lsxzm $module | grep "$2"
  done
  </code></pre>
  
  <p>lsxzm is a script that lists the contents of the compressed live Linux module.
  Now, a possible result for such a search would look like so (lets consider I saved the script as "/usr/local/bin/lsxzmgrep" and made it executable):</p>
  
  <pre><code>/porteus/base# lsxzmgrep . libtiff.so.3
  ./000-kernel.xzm
  ./001-core.xzm
  /usr/lib64/libtiff.so.3
  /usr/lib64/libtiff.so.3.9.7
  ./001-de-core_locales.xzm
  ./002-xorg.xzm
  ./003-xfce.xzm
  ./010-nvidia-304.137-k.4.16.3-x86_64-don.xzm
  </code></pre>
  
  <p>I would like to have only this output:</p>
  
  <pre><code>/porteus/base# lsxzmgrep . libtiff.so.3
  ./001-core.xzm
  /usr/lib64/libtiff.so.3
  /usr/lib64/libtiff.so.3.9.7
  </code></pre>
  
  <p>How can this be done?
  All I can think of is temporarily buffering the output of each grep from the for do done loop, and only print it together with the name of the module when grep did find something, done by examining the $? of grep.</p>
  
  <p>But that would be a less than elegant solution.</p>
  
  <p>Now I wonder, is a shell script, especially for bash 4, able to get the same result without the need of buffering the grep result and examining the return value of the grep command to get the same result: only print the name of the module and the listed file name(s) when the grep command did actually find something?</p>