Advertisement
cd62131

PowerShell grep -f

Oct 20th, 2016
359
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#
  2. .SYNOPSIS
  3. Grep-like search command
  4.  
  5. .DESCRIPTION
  6. This script works like 'grep -f FILE'.
  7.  
  8. .PARAMETER Directory
  9. target directory (default '.')
  10.  
  11. .PARAMETER Keyword
  12. path name to keyword file (default 'keyword.txt')
  13. #>
  14.  
  15. Param (
  16.     [parameter(Mandatory=$false)][String]$Directory='.'
  17.     , [parameter(Mandatory=$false)][String]$Keyword='keyword.txt'
  18. )
  19.  
  20. $Keywords = Get-Content $Keyword
  21. Get-ChildItem $Directory -Filter '*.csv' | ForEach-Object {
  22.     $File = $_.Name
  23.     Write-Output $File
  24.     $Keywords | ForEach-Object {
  25.         $Count = (Select-String -Pattern $_ -Path $File | Measure-Object).Count
  26.         if ($Count -gt 0) {
  27.             Write-Output ${_}:$Count
  28.         }
  29.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement