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

[玩转系统] PowerShell 检查文件是否包含字符串 [6 种方法]

作者:精品下载站 日期:2024-12-14 05:28:50 浏览:14 分类:玩电脑

PowerShell 检查文件是否包含字符串 [6 种方法]


[玩转系统] PowerShell 检查文件是否包含字符串 [6 种方法]

使用Select-String Cmdlet

使用 Select-String cmdlet 检查指定文件是否包含 PowerShell 中的给定字符串。

使用选择字符串 Cmdlet:

$path = "E:\Test\file1.txt"
$string = "customers"
$result = Select-String $path -Pattern $string
if ($result -ne $null) {
    Write-Host "The specified text file contains the string '$string'."
} else {
    Write-Host "The specified text file does not contain the string '$string'."
}

输出 :

The specified text file contains the string 'customers'.

首先,我们声明并初始化了一个名为 $path 的变量,其中包含 file1.txt 文件的位置。然后,我们创建了另一个名为 $string 的变量,并使用 "customers" 设置其值;这是我们在 file1.txt 中寻找的值。

之后,我们使用带有 -Pattern 参数的 Select-String 在位于 $path 的文件中搜索指定的 $string 并保存$result 变量中的返回值。

Select-String cmdlet 用于在其他字符串和文件中查找指定字符串;我们可以使用我们正在寻找的确切字符串或定义正则表达式来匹配模式。我们使用 -Pattern 参数来提及搜索字符串或模式。

Select-String 取决于文本的行;默认情况下,它会在文本的每一行中查找第一个匹配项,并且对于每个匹配项,它都会显示文件名、行号和具有匹配项的完整文本。由于我们只关心是否找到搜索字符串,因此我们使用 if-else 块和 -ne 运算符来检查 $result 是否找到code> 变量不等于 $null

如果没有,我们使用 Write-Host cmdlet 打印一条消息,说明已找到该字符串;否则,我们会打印一条消息,通知找不到该字符串。

Select-String cmdlet 类似于 UNIX 中的 grep 命令或 Windows 操作系统中的 findstr.exe

默认情况下,Select-String-Pattern 选项执行不区分大小写的匹配,这意味着如果我们替换 $string =,上面的代码将正常工作“客户”$string=“CUSTOMERS”。但是,如果您的项目要求区分大小写匹配,我们可以使用 -CaseSensitive 参数,如下所示。

将 Select-String 与 -CaseSensitive 一起使用:

$path = "E:\Test\file1.txt"
$string = "Customers"
$result = Select-String $path -Pattern $string -CaseSensitive
if ($result -ne $null) {
    Write-Host "The specified text file contains the string '$string'."
} else {
    Write-Host "The specified text file does not contain the string '$string'."
}

输出 :

The specified text file does not contain the string 'Customers'.

这次我们找不到 $string,因为我们有 customers,而不是 file1.txt 中的 Customers。这是由于 -CaseSensitive 参数造成的,该参数进行区分大小写的匹配并相应地显示消息。

对于上面的两个代码示例,-Pattern 由于其默认行为将被视为正则表达式,但我们没有指定正则表达式,而是指定一个简单的字符串。因此,在本例中,我们希望将 $string 解释为简单字符串。为此,我们可以使用-SimpleMatch 参数,如下所示。

将 Select-String 与 -SimpleMatch 结合使用:

$path = "E:\Test\file1.txt"
$string = "customers"
$result = Select-String -Path $path -Pattern $string -SimpleMatch -Quiet
if ($result){
    Write-Host "The specified text file contains the string '$string'."
}
else{
    Write-Host "The specified text file does not contain the string '$string'."
}

输出 :

The specified text file contains the string 'customers'.

此示例与上一个示例类似。但在这里,我们使用了 -SimpleMatch 参数,该参数不是强制性的,但使用它会指定模式中的 $string 不得被解释为正则表达式。

另外,我们使用了-Quiet参数让Select-String返回一个布尔值(True/False) 而不是包含文件名、行号和包含匹配项的整行的 MatchInfo 对象。

使用 Get-Content Cmdlet

我们可以使用 Get-Content cmdlet 和 Select-String cmdlet、Contains() 方法和 检查文件是否包含字符串>-match 运算符。下面让我们逐一了解一下。

Get-ContentSelect-String Cmdlet 结合使用

Get-ContentSelect-String cmdlet 结合使用,检查文件是否包含 PowerShell 中的指定字符串。

将 Get-Content 与 Select-String 结合使用:

$path = "E:\Test\file1.txt"
$string = "customers"
$result = Get-Content $path | Select-String $string -Quiet
if ($result) {
    Write-Host "The specified text file contains the string '$string'."
}
else{
    Write-Host "The specified text file does not contain the string '$string'."
}

输出 :

The specified text file contains the string 'customers'.

我们已经了解了 $path$string$result 变量、Select-String cmdlet 和 -Quiet 参数。我们在本示例中使用的新功能是 Get-Content cmdlet,它用于检索给定位置处项目的内容;例如,函数的内容或文本文件中的文本。

当我们处理文本文件时,了解此 cmdlet 如何处理文本文件非常重要。首先,它从给定的文本文件中一次读取一行并返回对象的集合,其中每个对象表示一行内容。之后,Select-String 搜索匹配项,如果找到匹配项,则返回 True。最后,Select-String cmdlet 返回布尔值,因为我们使用了之前学过的-Quiet 参数。

如果您使用的是 PowerShell 3.0,您还可以获取从指定项目的开头/结尾开始的 n 行数。

Get-ContentContains() 方法结合使用

使用 Get-ContentContains() 方法检查文件是否包含 PowerShell 中指定的字符串。

将 Get-Content 与 Contains() 方法结合使用:

$path = "E:\Test\file1.txt"
$string = "customers"
$fileContent = Get-Content $path
if ($fileContent.Contains($string)) {
    Write-Host "The specified text file contains the string '$string'."
}
else{
    Write-Host "The specified text file does not contain the string '$string'."
}

输出 :

The specified text file contains the string 'customers'.

我们已经在前面的示例中了解了 Get-Content;让我们扩展该讨论,说 Get-Content cmdlet 检索 file1.txt 的内容并将其作为字符串对象返回。现在,我们可以使用处理字符串的方法。

其中之一是 Contains() 方法,该方法与 $fileContent (字符串对象)链接,将 $string 作为参数并检查 $fileContent 是否包含 $string。如果是,$fileContent.Contains($string) 表达式将返回 True 并导致 if 块执行;否则,else 块将被执行。

请记住,Contains() 方法区分大小写,这意味着 Customercustomer 是两个不同的值。请参阅以下示例。

将 Get-Content 与 Contains() 方法结合使用:

$path = "E:\Test\file1.txt"
$string = "Customers"
$fileContent = Get-Content $path
if ($fileContent.Contains($string)) {
    Write-Host "The specified text file contains the string '$string'."
}
else{
    Write-Host "The specified text file does not contain the string '$string'."
}

输出 :

The specified text file does not contain the string 'Customers'.

Get-Content-match 运算符结合使用

使用 Get-Content-match 运算符检查文件是否包含 PowerShell 中指定的字符串。

将 Get-Content 与 -match 运算符结合使用:

$path = "E:\Test\file1.txt"
$string = "customers"
If (Get-Content $path | %{$_ -match "customers"}){
    Write-Host "The specified text file contains the string '$string'."
}
else{
    Write-Host "The specified text file does not contain the string '$string'."
}

输出 :

The specified text file contains the string 'customers'.

在这里,我们使用相同的逻辑,但使用不同的运算符,-match 将文本中的每个单词(使用 Get-Content 检索)与 $string 匹配代码>.如果 file1.txt 包含 $string,则 if 中包含的表达式返回 True;否则,它返回False

-match 运算符执行不区分大小写的匹配,因此 customersCustomers 相同。

使用 Get-ChildItem Cmdlet

Get-ChildItem cmdlet 还可以与 Select-String 一起使用来检查文件是否具有指定的字符串。此外,我们可以将此 cmdlet 与 Select-StringForEach 结合使用来搜索多个文本文件中的字符串数组。让我们看看这些方法是如何工作的。

Get-ChildItem Cmdlet 与 Select-String Cmdlet 结合使用

Get-ChildItemSelect-String cmdlet 结合使用,检查文件是否包含 PowerShell 中的指定字符串。

将 Get-ChildItem 与 Select-String 结合使用:

$path = "E:\Test\"
$string = "client"
Get-ChildItem -Path $path -Recurse | Select-String -Pattern $string

输出 :

file2.txt:1:This is file2 and has some sample text for the client.
file4.txt:1:This is file2 and has some sample text for the client.

在这里,我们使用 Get-ChildItem 以及 -Path-Recurse 参数来检索指定 $path 中的所有项目和子项目-Path 参数用于提及路径,而 -Recurse 用于获取给定 $path 中的所有项目及其所有子项目。 Get-ChildItem -Path $path -Recurse 的结果如下:

Get-ChildItem 的输出:

Directory: E:\Test
Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a----          3/7/2023   8:16 AM             60 file1.txt
-a----          3/7/2023   8:16 AM             57 file2.txt
-a----          3/7/2023   8:16 AM             90 file3.txt
-a----          3/7/2023   8:16 AM             57 file4.txt

上述结果通过管道传输到 Select-String cmdlet,该 cmdlet 搜索指定的 $string 并返回文件名、行号以及包含 $string 的整行>$string 如上面的输出所示。

Get-ChildItemSelect-StringForEach 结合使用

检查文件是否包含该字符串:

  • 使用数组运算符创建字符串数组。
  • 将路径保存在变量中后,使用 ForEach 循环迭代字符串数组。
  • 在每次迭代中:

    • 使用 Get-ChildItem 检索指定路径中的所有 .txt 文件。
  • 使用 Select-String cmdlet 查找搜索字符串,并使用 Select cmdlet 选择路径、行号和匹配的单词。

将 Get-ChildItem 与 Select-String 和 ForEach 结合使用:

$searchStrings = @('customers','client')
$path = "E:\Test\"
Foreach ($ss in $searchStrings){
    Get-Childitem -Path $path -Recurse -include "*.txt" | 
    Select-String -Pattern "$ss" | 
    Select Path,LineNumber,@{n='SearchWord';e={$ss}}
}

输出 :

Path              LineNumber SearchWord
----              ---------- ----------
E:\Test\file1.txt          1 customers
E:\Test\file2.txt          1 client
E:\Test\file4.txt          1 client

由于我们使用Select-String来查找搜索字符串,因此它将执行不区分大小写的搜索;请参见以下示例。

将 Get-ChildItem 与 Select-String 和 ForEach 结合使用:

$searchStrings = @('Customers','CLIENT')
$path = "E:\Test\"
Foreach ($ss in $searchStrings){
    Get-Childitem -Path $path -Recurse -include "*.txt" | 
    Select-String -Pattern "$ss" | 
    Select Path,LineNumber,@{n='SearchWord';e={$ss}}
}

输出 :

Path              LineNumber SearchWord
----              ---------- ----------
E:\Test\file1.txt          1 Customers
E:\Test\file2.txt          1 CLIENT
E:\Test\file4.txt          1 CLIENT

这就是 PowerShell 检查文件是否包含字符串的全部内容。

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

取消回复欢迎 发表评论:

关灯