当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 如何使用 PowerShell Grep 等效的 Select-String

作者:精品下载站 日期:2024-12-14 03:42:48 浏览:15 分类:玩电脑

如何使用 PowerShell Grep 等效的 Select-String


当您需要在 Linux 中搜索字符串或日志文件时,我们可以使用 grep 命令。对于 PowerShell,我们可以使用 grep 等效项 Select-String。使用这个强大的 cmdlet,我们可以获得几乎相同的结果。

Select-String 使用类似于 grep 正则表达式来查找文件和字符串中的文本模式。它可以搜索多个文件并报告位置,包括每个文件的字符串的行号。也可以仅查找字符串的第一次出现,或者例如仅返回与字符串不匹配的文件。

在本文中,我们将了解 PowerShell grep 等效项 Select-String。我们将看一下可用于通过 PowerShell 查找字符串的不同示例。请务必通读最后,以获得不错的小奖励提示!

使用 PowerShell Select-String 查找字符串

在我们深入研究 select-string cmdlet 的所有可能性之前,我们首先看一下几个常见示例。要在日志文件中搜索特定字符串,我们可以在 PowerShell 中使用以下 cmdlet:

# Search for the string error in the path
Select-String -Pattern "error" -Path "C:\temp\log\*.log"

# or in short:
Select-String "error" "C:\temp\log\*.log"

grep 等效项是:

grep "error" .\log\*.log

默认情况下,select-string cmdlet 的输出将显示文件名、行号以及找到该字符串的完整行:

[玩转系统] 如何使用 PowerShell Grep 等效的 Select-String

PowerShell的优点是我们可以轻松地将输出格式化为更可读的格式。例如,如果您只对文件名、路径和行号感兴趣,那么您可以将结果格式化为表格并仅选择您需要的字段:

Select-String -Pattern "error" -Path "C:\temp\log\*.log" | Select LineNumber, FileName, Path

# Result:
LineNumber Filename         Path
---------- --------         ----
         9 01032022-app.log C:\temp\log032022-app.log
        18 04032022-app.log C:\temp\log032022-app.log
         7 05032022-app.log C:\temp\log032022-app.log
         9 11022022-app.log C:\temp\log022022-app.log
         9 11032021-app.log C:\temp\log032021-app.log
         9 11102021-app.log C:\temp\log102021-app.log
        38 11102021-app.log C:\temp\log102021-app.log
         9 15032022-app.log C:\temp\log032022-app.log

选择字符串参数

我们已经快速了解了 cmdlet 的基本用法,现在让我们更深入地了解 PowerShell 中搜索字符串 cmdlet 的所有可能性。该 cmdlet 附带了几个参数,可帮助我们搜索字符串并选择结果:

-Pattern

要查找的文本或字符串。接受正则表达式

-Path

指定要搜索的文件或文件路径。接受通配符

-CaseSensitive

搜索字符串区分大小写

-Context

在图案之前和/或之后显示线条

-Quiet

只返回 true 或 false

-Raw

像 grep 一样只返回匹配的字符串

-List

仅列出文件中的第一个匹配项。用于获取具有模式的文件列表

-SimpleMatch

将模式视为简单字符串

-Include

指定要包含在搜索中的文件(“*.txt”、“*.log”)

-Exclude

指定要从搜索路径中排除的文件

-NotMatch

仅返回与模式不匹配的项目

-AllMatches

返回结果的所有匹配项

使用上下文显示前后行

Select-String cmdlet 返回找到我们搜索的字符串的行。但是,例如,在搜索日志文件时,您可能还想知道错误之前或之后发生的情况。

这就是 -context 参数的用武之地。它允许您指定匹配行和/或匹配行之后需要返回的行数。第一个数字是前面的行数,第二个数字是后面的行数。如果您只提供一位数字,那么它将用于两者。

# Return 3 lines before
Select-String -Pattern "error" -Path "C:\temp\log\sub\*.log" -Context 3,0

# Return 2 lines before and 2 lines after the error:
Select-String -Pattern "error" -Path "C:\temp\log\sub\*.log" -Context 2

与模式匹配的行用 > 指示,并且该字符串突出显示:

[玩转系统] 如何使用 PowerShell Grep 等效的 Select-String

使用 Quiet 返回 True 或 False

当在脚本中使用 PowerShell grep 等效的选择字符串时,有时您只需要知道该字符串是否存在。默认情况下,cmdlet 将返回 MatchInfo 对象。具有行号、路径、字符串等的对象。

要检查日志文件是否包含错误,我们可以执行以下操作:

# Returns true or False
Select-String -Pattern "error" -Path "C:\temp\log\sub\*.log" -Quiet

# Result:
True

仅返回与 Raw 匹配的字符串

如果您习惯使用 Linux grep 命令,那么您可能会注意到 select-string 的结果略有不同。 PowerShell 等效项返回一个对象,而不是 grep 命令仅返回字符串。

当您将 -raw 参数添加到命令中时,cmdlet 将仅返回与模式匹配的字符串:

Select-String -Pattern "error" -Path "C:\temp\log\sub\*.log" -Raw

# Result:
2022-03-01 10:59:21 [ERROR] - String failed to be a string

搜索多种文件类型

路径参数已经接受通配符,允许我们搜索多个文件。但在这种情况下,我们只能搜索一种文件类型。使用 -include 参数时,我们可以指定要搜索的多种文件类型。

要使用该参数,我们仍然需要提供路径,否则 cmdlet 将在脚本根位置中搜索。

# Search in the txt and log files
Select-String -Pattern "error" -Path "C:\temp\log\sub\*" -Include "*.txt","*.log"

# Result
C:\temp\log\sub\LT3452-process.log:4:2022-03-01 10:59:21 [ERROR] - String failed to be a string
C:\temp\log\sub\LT4423-process.txt:3:2022-02-01 05:49:21 [ERROR] - Squirrel failed to find process

还可以使用 -exclude 参数排除文件扩展名。

仅列出带有 Select-String 的文件

使用 select-string cmdlet 搜索多个文件时,PowerShell 将返回找到的字符串的所有实例。但有时您只想知道日志文件等是否包含错误。

如果我们在日志文件中搜索模式错误,我们可以看到文件 11102021-app.log 被找到两次:

Select-String -Pattern "error" -Path "C:\temp\log\*" | Select LineNumber, Filename

# Result:
LineNumber Filename
---------- --------
         9 01032022-app.log
        18 04032022-app.log
         7 05032022-app.log
         9 11022022-app.log
         9 11032021-app.log
         9 11102021-app.log
        38 11102021-app.log
         9 15032022-app.log

通过使用参数 -List 仅返回第一个实例,从而生成包含匹配模式的唯一文件名列表:

Select-String -Pattern "error" -Path "C:\temp\log\*" -List | Select LineNumber, Filename

[玩转系统] 如何使用 PowerShell Grep 等效的 Select-String

返回没有模式的行

一直以来,我们一直在日志文件中搜索包含字符串“error”的字符串。但如果你想得到相反的结果怎么办?所有不包含“错误”一词的

为此,我们可以使用 -NotMatch 参数。这将返回路径中与我们提供的模式不匹配的所有行:

Select-String -Pattern "error" -Path "C:\temp\log\*" -NotMatch | Select LineNumber, Filename

查找不匹配模式的文件

Select-String cmdlet 在行级别上搜索文件。这意味着我们不能简单地使用此 cmdlet 来仅返回不包含特定字符串的文件。

要获取所有不包含该字符串的文件,我们需要使用 Get-ChildItem cmdlet,并在每个文件上使用 Select-String cmdlet。

通过使用 -quiet 参数,我们可以返回 true 或 false,从而允许我们仅选择(并返回)不包含该模式的文件:

Get-ChildItem -path "C:\temp\log\*" | Where-Object { !( $_ | Select-String -Pattern "error" -Quiet ) }

# Result:

    Directory: C:\temp\log

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d----           15-3-2022    13:52                sub
-a---           15-3-2022    14:09           4120 04032022-app.log
-a---           15-3-2022    12:59           2860 11032022-app.log

PowerShell 选择字符串递归

PowerShell 中的 select-string cmdlet 仅在给定目录中搜索。即使您使用通配符,它也不会遍历子文件夹。要搜索所有子文件夹,我们需要将 cmdlet 与 Get-ChildItem 结合起来。

因此,要搜索所有子文件夹,我们首先将使用参数 -Recurse 进行递归搜索,通过 Get-Childitem 获取所有项目。然后将每个文件传递给 Select-String cmdlet:

Get-ChildItem -path "C:\temp\log\*" -Recurse | Select-String -Pattern "error" | Select LineNumber, Filename, Path

[玩转系统] 如何使用 PowerShell Grep 等效的 Select-String

PowerShell 选择字符串多种模式

在搜索其他文本文件的日志文件时,有时需要搜索多种模式。现在,您可以简单地运行 Select-String cmdlet 两次,但这并不是非常高效。

幸运的是,我们可以为 Select-String cmdlet 提供多种模式。您可以简单地创建一个包含字符串或逗号分隔字符串的数组:

Select-String -Pattern "error","warning" -Path "C:\temp\log\*" -List | Select LineNumber, Filename, Pattern

[玩转系统] 如何使用 PowerShell Grep 等效的 Select-String

正如您所看到的,我已将属性 Pattern 添加到 select 语句中。这样我们就可以看到文件中的哪个模式以及哪一行匹配。

总结

PowerShell grep 等效的 Select-String 是一个很好的工具,可以在文本文件或其他输出流中查找字符串。如果您真的喜欢使用 grep 命令,那么我有一个小技巧给您。您可以在 PowerShell 中创建新别名,以便在键入 grep 时使用 select-string cmdlet。

要创建别名类型:

new-alias grep select-string

现在你可以像在 Linux 中一样使用 grep 了:

[玩转系统] 如何使用 PowerShell Grep 等效的 Select-String

我希望这篇文章对您有用,如果您有任何疑问,请在下面发表评论。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯