title: Powershell: Count instances of strings in a file using a list
body: <p>I am trying to get the number of times a string (varying from 40 to 400+ characters) in "file1" occurs in "file2" in an effective way.  file1 has about 2k lines and file2 has about 130k lines.  I currently have a Unix solution that does it in about 2 mins in a VM and about 5 in <a href="http://www.cygwin.com/" rel="nofollow">Cygwin</a>, but I am trying to do it with Powershell/Python since the files are in windows and I am using the output in excel and use it with automation (AutoIT.)<br>
  I have a solution, but it takes WAY too long (in about the same times that the Cygwin finished - all 2k lines - I had only 40-50 lines in Powershell!)
  Although I haven't prepare a solution yet, I am open to use Python as well if there is a solution that can be fast and accurate.</p>
  
  <p>Here is the Unix Code:</p>
  
  <pre><code>while read SEARCH_STRING; 
  do printf "%s$" "${SEARCH_STRING}"; 
  grep -Fc "${SEARCH_STRING}" file2.csv; 
  done &lt; file1.csv | tee -a output.txt;
  </code></pre>
  
  <p>And here is the Powershell code I currently have</p>
  
  <pre><code>$Target = Get-Content .\file1.csv
  Foreach ($line in $Target){
      #Just to keep strings small, since I found that not all
      #strings were being compared correctly if they where 250+ chars
      $line = $line.Substring(0,180)
      $Coll = Get-Content .\file2.csv | Select-string -pattern "$line"
      $cnt = $Coll | measure
      $cnt.count
  }
  </code></pre>
  
  <p>Any ideas of suggestions will help. </p>
  
  <p>Thanks.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I'm trying a modified solution suggested by C.B. </p>
  
  <pre><code>del .\output.txt
  $Target = Get-Content .\file1.csv
  $file= [System.IO.File]::ReadAllText( "C:\temp\file2.csv" )
  Foreach ($line in $Target){
      $line = [string]$line.Substring(0, $line.length/2)
      $cnt = [regex]::matches( [string]$file, $line).count  &gt;&gt; ".\output.txt" 
  }
  </code></pre>
  
  <p>But, since my strings in file1 are varying in length I keept getting OutOfBound exceptions for the SubString function, so I halved (/2) the input string to try to get a match. And when I try to halve them, if I it had an open parentheses, it tells me this:</p>
  
  <pre><code>Exception calling "Matches" with "2" argument(s): "parsing "CVE-2013-0796,04/02/2013,MFSA2013-35 SeaMonkey: WebGL
  crash with Mesa graphics driver on Linux (C" - Not enough )'s."
  At C:\temp\script_test.ps1:6 char:5
  +     $cnt = [regex]::matches( [string]$file, $line).count  &gt;&gt; ".\output.txt ...
  +     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
  + FullyQualifiedErrorId : ArgumentException
  </code></pre>
  
  <p>I don't know if there is a way to raise the input limit in powershell (My biggest size at the moment is 406, but could be bigger in the future) or just give up and try a Python solution.</p>
  
  <p>Thoughts?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Thanks to @C.B. I got the correct answer and it matches the output of the Bash script perfectly. Here is the full code that outputs results to a text file:</p>
  
  <pre><code>$Target = Get-Content .\file1.csv
  $file= [System.IO.File]::ReadAllText( "C:\temp\file2.csv" )
  Foreach ($line in $Target){
      $cnt = [regex]::matches( $file, [regex]::escape($line)).count  &gt;&gt; ".\output.txt"    
  }
  </code></pre>