PowerShell equivalent to grep -f

前端 未结 9 1614
情书的邮戳
情书的邮戳 2020-12-22 16:09

I\'m looking for the PowerShell equivalent to grep --file=filename. If you don\'t know grep, filename is a text file where each line has a regular

9条回答
  •  礼貌的吻别
    2020-12-22 16:29

    but select-String doesn't seem to have this option.

    Correct. PowerShell is not a clone of *nix shells' toolset.

    However it is not hard to build something like it yourself:

    $regexes = Get-Content RegexFile.txt | 
               Foreach-Object { new-object System.Text.RegularExpressions.Regex $_ }
    
    $fileList | Get-Content | Where-Object {
      foreach ($r in $regexes) {
        if ($r.IsMatch($_)) {
          $true
          break
        }
      }
      $false
    }
    

提交回复
热议问题