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

[玩转系统] PowerShell 的 Grep |通过示例快速浏览 PowerShell Grep

作者:精品下载站 日期:2024-12-14 04:54:21 浏览:19 分类:玩电脑

PowerShell 的 Grep |通过示例快速浏览 PowerShell Grep


[玩转系统] PowerShell 的 Grep |通过示例快速浏览 PowerShell Grep

PowerShell Grep 简介

Grep 命令不是 PowerShell cmdlet。它是 Linux/Unix 中使用的命令,允许用户根据各种模式进行过滤。在 PowerShell 中通过 Select-String cmdlet 可以实现相同的效果。它可以被认为是 Windows 中的 GREP 等效项。 Select-String 用于比较两个文件中的文本和模式以及输入字符串。它使用正则表达式来进行匹配。它逐行搜索模式。每当在一行中找到匹配项时,就会打印相应的文件名、行号和匹配项。在搜索时,如果用户想要使用编码进行比较,甚至可以指定该编码。

句法

以下是 Select-String 的语法:

NAME
Select-String
SYNTAX
Select-String [-Pattern] <string[]> [-Path] <string[]> [-SimpleMatch] [-CaseSensitive] [-Quiet] [-List] [-Include <string[]>] [-Exclude <string[]>] [-NotMatch]
[-AllMatches] [-Encoding {unicode | utf7 | utf8 | utf32 | ascii | bigendianunicode | default | oem}] [-Context <int[]>]  [<CommonParameters>]
Select-String [-Pattern] <string[]> -InputObject<psobject> [-SimpleMatch] [-CaseSensitive] [-Quiet] [-List] [-Include <string[]>] [-Exclude <string[]>] [-NotMatch]
[-AllMatches] [-Encoding {unicode | utf7 | utf8 | utf32 | ascii | bigendianunicode | default | oem}] [-Context <int[]>]  [<CommonParameters>]
Select-String [-Pattern] <string[]> -LiteralPath<string[]> [-SimpleMatch] [-CaseSensitive] [-Quiet] [-List] [-Include <string[]>] [-Exclude <string[]>] [-NotMatch]
[-AllMatches] [-Encoding {unicode | utf7 | utf8 | utf32 | ascii | bigendianunicode | default | oem}] [-Context <int[]>]  [<CommonParameters>]
ALIASES
sls

参数

下面是PowerShell Grep的不同参数

1. -AllMatches:这表示必须返回每行中的所有匹配模式。如果没有这个,Select-String 只匹配一行中的第一个匹配模式。其类型为开关参数。它的默认值为 false。它不接受管道输入,也不接受通配符。

2. -CaseSensitive:表示必须区分大小写匹配。默认情况下,匹配不区分大小写。其类型为开关参数。它的默认值为 false。它不接受管道输入,也不接受通配符。

3. -上下文:这捕获找到匹配项之前的行数以及匹配项之后的行数。如果只提到一个数字,则该数字决定比赛之前和之后的行数。如果提到两个数字,第一个数字表示比赛之前的行数,而比赛之后的行数由第二个数字确定。它的类型是int32[]。它的默认值是无。它不接受管道输入,也不接受通配符。

4. -Culture:表示用于匹配目的的区域性名称。要获取可用区域性的列表,可以使用 Get-Culture -ListAvailable cmdlet。它首先是在 PowerShell 7 中引入的。它的类型是字符串。默认值是当前 PS 会话的文化值。它不接受管道输入,也不接受通配符。

5. -Encoding:表示目标文件的编码类型。默认编码类型为UTF8NoBOM。其他可接受的值包括 ASCII、BigEndianUnicode、OEM、Unicode、UTF7、UTF8、UTF8BOM、UTF8NoBOM、UTF32。它不接受管道输入,也不接受通配符。

6. -排除:这表示需要从匹配中排除的项目列表。它可以是路径或模式。它的类型是String[]。它的默认值是无。它可以接受通配符,但不允许管道输入。

7. -包含:这表示需要包含以进行匹配的项目列表。它可以是路径或模式。它的类型是String[]。它的默认值是无。它可以接受通配符,但不允许管道输入。

8. -InputObject:表示要搜索的文本。它可以是保存文本或表达式的变量。它的类型是PSObject。它的默认值是无。它接受管道输入,但不接受通配符。

9. -List:使用此选项时,仅考虑并返回每个文件的第一个匹配实例。其类型为开关参数。它的默认值为 false。它不接受管道输入,也不允许使用通配符。

10. -LiteralPath:表示要搜索的文件的路径。该值与键入的方式相同。不允许使用通配符,如果路径有特殊字符,则必须用单引号引起来。它的类型是string[]。它的别名是 PSPath 和 LP。它的默认值是无。它接受管道输入,但不接受通配符。

11. -NoEmphasis:默认情况下选择与搜索模式匹配的字符串,若要禁用突出显示,请使用此参数。这是在PowerShell 7中首次引入的。它的类型是switch参数。它的默认值为 false。它不接受管道输入,也不接受通配符。

12. -Pattern:这表示需要搜索的模式。它被视为正则表达式。它的类型是String[]。它的默认值是无。它不接受管道输入,也不允许使用通配符。

13. -Path:表示要搜索的文件的路径。它的默认值是当前目录。它接受管道输入;此外,还接受通配符。

14. -NotMatch:用于查找不匹配的情况。其类型为开关参数。它的默认值为 false。它不接受管道输入,也不允许使用通配符。

PowerShell Grep 示例

下面给出了示例:

代码:

Write-Host "Welcome to Select String example in Powershell"
$input=@("one","two","three","four","ttt","thousand","theew")
Write-Host "Demo of select in an array"
$input| Select-String -Pattern 'th'
Write-Host "Searching a single file"
Select-String -Pattern Scri -Path "C:\Vignesh\Test\AD Group export to csv.txt"
Write-Host "Matching multiple files"
Get-ChildItem C:\Vignesh\Test\*.txt -Recurse |Select-String -Pattern Scri
Write-Host "Finding the number of non matches"
$result=Select-String -Pattern Scri -NotMatch -Path "C:\Vignesh\Test\AD Group export to csv.txt"
Write-Host "Total number of non matches" $result.Count

输出:

[玩转系统] PowerShell 的 Grep |通过示例快速浏览 PowerShell Grep

结论

因此,本文详细解释了如何使用 Select-String cmdlet 在 PowerShell 中实现 grep。它解释了 cmdlet 可用的各种类型的参数以及适当的示例。

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

取消回复欢迎 发表评论:

关灯