title: How to create a backup of files' lines containing "foo"
body: <p>Basically I have a directory and sub-directories that needs to be scanned to find .csv files. From there I want to copy all lines containing "foo" from the csv's found to new files (in the same directory as the original) but with the name reflecting the file it was found in.</p>
  
  <p>So far I have</p>
  
  <pre><code>find -type f -name "*.csv" | xargs egrep -i "foo" &gt; foo.csv
  </code></pre>
  
  <p>which yields one backup file (foo.csv) with everything in it, and the location it was found in is part of the data. Both of which I don't want.</p>
  
  <p>What I want:</p>
  
  <p>For example if I have:</p>
  
  <pre><code>csv1.csv
  csv2.csv
  </code></pre>
  
  <p>and they both have lines containing "foo", I would like those lines copied to:</p>
  
  <pre><code>csv1_foo.csv
  csv2_foo.csv
  </code></pre>
  
  <p>and I don't anything extra entered in the backups, other than the full line containing "foo" from the original file. I.e. I don't want the original file name in the backup data, which is what my current code does.</p>
  
  <p>Also, I suppose I should note that I'm using egrep, but my example doesn't use regex. I will be using regex in my search when I apply it to my specific scenario, so this probably needs to be taken into account when naming the new file. If that seems too difficult, an answer that doesn't account for regex would be fine.</p>
  
  <p>Thanks ahead of time!</p>