title: linux merge only subset of lines from multiple files
body: <p>I have the following folder structure:</p>
  
  <pre><code>/drive1/180204_somerandomtext/file.csv
  /drive1/180504_somerandomtext/file.csv
  /drive1/190101_somerandomtext/file.csv
  /drive1/190305_somerandomtext/file.csv
  ...
  </code></pre>
  
  <p>Each file.txt has the same structure, but contains different data. From a file.txt I want to extract only a subset of lines using the following command:</p>
  
  <pre><code>grep -A5000 -m1 -e 'Sample_ID,' /drive1/180204_somerandomtext/file.csv | tail -n+2
  </code></pre>
  
  <p>This command works and prints the next 5000 lines following the line that starts with 'Sample_ID,'</p>
  
  <p>I've extended this command</p>
  
  <pre><code>grep -A5000 -m1 -e 'Sample_ID,' /drive1/180204_somerandomtext/file.csv | tail -n+2 | sed 's/^/180204_somerandomtext,/' 
  </code></pre>
  
  <p>Using the 'sed' I now add to the beginning of each line the pattern '180204_somerandomtext', which is actually the name of the folder that contains the file.csv</p>
  
  <p>I'm now stuck at the following steps:</p>
  
  <ul>
  <li>how to do this for all file.csv files in the subfolders of drive1</li>
  <li>how to store this result in one large file called 'samples.csv'</li>
  </ul>
  
  <p>I've tried something with xargs. It works with the grep command, but piping the sed, isn't working then.</p>
  
  <pre><code>find /drive1/ -maxdepth 1 -name '1*' | cut -d '/' -f2 | xargs -I {} grep -A5000 -m1 -e 'Sample_ID,' /drive1/{}/file.csv | sed 's/^/{},/'
  </code></pre>