我正在寻找PowerShell等价于grep——file=filename。如果你不知道grep, filename是一个文本文件,其中每一行都有一个你想要匹配的正则表达式模式。

也许我遗漏了一些明显的东西,但是Select-String似乎没有这个选项。


但是select-String似乎没有这个选项。

正确的。PowerShell不是*nix shell工具集的克隆。

然而,自己建造这样的东西并不难:

$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
}

我不熟悉grep,但选择字符串你可以做:

Get-ChildItem filename.txt | Select-String -Pattern <regexPattern>

你也可以使用Get-Content:

(Get-Content filename.txt) -match 'pattern'

Select-String中的-Pattern参数支持模式数组。所以你要找的是

Get-Content .\doc.txt | Select-String -Pattern (Get-Content .\regex.txt)

它通过使用regex.txt中的每个regex(每行一个)来搜索文本文件doc.txt


也许?

[regex]$regex = (get-content <regex file> |
foreach {
          '(?:{0})' -f $_
        }) -join '|'

Get-Content <filespec> -ReadCount 10000 |
 foreach {
           if ($_ -match $regex)
             {
              $true
              break
             }
         }

PS) new-alias grep findstr
PS) C:\WINDOWS> ls | grep -I -N exe

105:-a---        2006-11-02     13:34      49680 twunk_16.exe
106:-a---        2006-11-02     13:34      31232 twunk_32.exe
109:-a---        2006-09-18     23:43     256192 winhelp.exe
110:-a---        2006-11-02     10:45       9216 winhlp32.exe

PS) grep /?

所以我在这个链接上找到了一个很好的答案: https://www.thomasmaurer.ch/2011/03/powershell-search-for-string-or-grep-for-powershell/

但本质上是:

Select-String -Path "C:\file\Path\*.txt" -Pattern "^Enter REGEX Here$"

这在PowerShell的一行中提供了目录文件搜索(*或者您可以只指定一个文件)和文件内容搜索,非常类似于grep。输出将类似于:

doc.txt:31: Enter REGEX Here
HelloWorld.txt:13: Enter REGEX Here

这个问题已经有了答案,但我只想补充一点,在Windows中有Windows子系统for Linux WSL。

例如,如果你想检查名为Elasicsearch的服务是否处于运行状态,你可以在powershell中执行如下代码片段所示的操作

netstart | grep Elasticsearch


我有同样的问题,试图找到文件中的文本与powershell。我使用了以下方法——尽可能地接近Linux环境。

希望这对大家有所帮助:

PowerShell:

PS) new-alias grep findstr
PS) ls -r *.txt | cat | grep "some random string"

解释:

ls       - lists all files
-r       - recursively (in all files and folders and subfolders)
*.txt    - only .txt files
|        - pipe the (ls) results to next command (cat)
cat      - show contents of files comming from (ls)
|        - pipe the (cat) results to next command (grep)
grep     - search contents from (cat) for "some random string" (alias to findstr)

是的,这也可以:

PS) ls -r *.txt | cat | findstr "some random string"

我通过PowerShell的“filter”和“alias”找到了一个可能的方法,当你想在管道输出中使用grep时(grep文件应该类似):

首先定义一个过滤器:

filter Filter-Object ([string]$pattern) {
    Out-String -InputObject $_ -Stream | Select-String -Pattern "$pattern"
}

然后定义别名:


    New-Alias -Name grep -Value Filter-Object

最后,将前一个过滤器和别名放在你的配置文件中:

$ Home \ PowerShell \ Microsoft.PowerShell_profile.ps1[我]文档

重启你的PS,这样你就可以使用它了:

别名| grep“grep”


参考文献

别名: Set-Alias这里 在这里新别名 过滤器(特殊功能)在这里 概要文件(就像bash的.bashrc):这里 Out-string(这是关键): 在PowerShell Output中是基于对象的,所以键 是将对象转换为字符串并grep字符串。 选择string: 在字符串和文件中查找文本