[玩转系统] PowerShell 检查文件是否包含字符串 [6 种方法]
作者:精品下载站 日期:2024-12-14 05:28:50 浏览:14 分类:玩电脑
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-Content
与 Select-String
Cmdlet 结合使用
将 Get-Content
与 Select-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-Content
与 Contains()
方法结合使用
使用 Get-Content
和 Contains()
方法检查文件是否包含 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()
方法区分大小写,这意味着 Customer
和 customer
是两个不同的值。请参阅以下示例。
将 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
运算符执行不区分大小写的匹配,因此 customers
与 Customers
相同。
使用 Get-ChildItem
Cmdlet
Get-ChildItem
cmdlet 还可以与 Select-String
一起使用来检查文件是否具有指定的字符串。此外,我们可以将此 cmdlet 与 Select-String
和 ForEach
结合使用来搜索多个文本文件中的字符串数组。让我们看看这些方法是如何工作的。
将 Get-ChildItem
Cmdlet 与 Select-String
Cmdlet 结合使用
将 Get-ChildItem
与 Select-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-ChildItem
与 Select-String
和 ForEach
结合使用
检查文件是否包含该字符串:
- 使用数组运算符创建字符串数组。
- 将路径保存在变量中后,使用
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 检查文件是否包含字符串的全部内容。
猜你还喜欢
- 03-30 [玩转系统] 如何用批处理实现关机,注销,重启和锁定计算机
- 02-14 [系统故障] Win10下报错:该文件没有与之关联的应用来执行该操作
- 01-07 [系统问题] Win10--解决锁屏后会断网的问题
- 01-02 [系统技巧] Windows系统如何关闭防火墙保姆式教程,超详细
- 12-15 [玩转系统] 如何在 Windows 10 和 11 上允许多个 RDP 会话
- 12-15 [玩转系统] 查找 Exchange/Microsoft 365 中不活动(未使用)的通讯组列表
- 12-15 [玩转系统] 如何在 Windows 上安装远程服务器管理工具 (RSAT)
- 12-15 [玩转系统] 如何在 Windows 上重置组策略设置
- 12-15 [玩转系统] 如何获取计算机上的本地管理员列表?
- 12-15 [玩转系统] 在 Visual Studio Code 中连接到 MS SQL Server 数据库
- 12-15 [玩转系统] 如何降级 Windows Server 版本或许可证
- 12-15 [玩转系统] 如何允许非管理员用户在 Windows 中启动/停止服务
取消回复欢迎 你 发表评论:
- 精品推荐!
-
- 最新文章
- 热门文章
- 热评文章
[影视] 黑道中人 Alto Knights(2025)剧情 犯罪 历史 电影
[古装剧] [七侠五义][全75集][WEB-MP4/76G][国语无字][1080P][焦恩俊经典]
[实用软件] 虚拟手机号 电话 验证码 注册
[电视剧] 安眠书店/你 第五季 You Season 5 (2025) 【全10集】
[电视剧] 棋士(2025) 4K 1080P【全22集】悬疑 犯罪 王宝强 陈明昊
[软件合集] 25年6月5日 精选软件22个
[软件合集] 25年6月4日 精选软件36个
[短剧] 2025年06月04日 精选+付费短剧推荐33部
[短剧] 2025年06月03日 精选+付费短剧推荐25部
[软件合集] 25年6月3日 精选软件44个
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电视剧] 欢乐颂.5部全 (2016-2024)
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[影视] 【稀有资源】香港老片 艺坛照妖镜之96应召名册 (1996)
[剧集] 神经风云(2023)(完结).4K
[剧集] [BT] [TVB] [黑夜彩虹(2003)] [全21集] [粤语中字] [TV-RMVB]
[实用软件] 虚拟手机号 电话 验证码 注册
[资源] B站充电视频合集,包含多位重量级up主,全是大佬真金白银买来的~【99GB】
[影视] 内地绝版高清录像带 [mpg]
[书籍] 古今奇书禁书三教九流资料大合集 猎奇必备珍藏资源PDF版 1.14G
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电影] 美国队长4 4K原盘REMUX 杜比视界 内封简繁英双语字幕 49G
[电影] 死神来了(1-6)大合集!
[软件合集] 25年05月13日 精选软件16个
[精品软件] 25年05月15日 精选软件18个
[绝版资源] 南与北 第1-2季 合集 North and South (1985) /美国/豆瓣: 8.8[1080P][中文字幕]
[软件] 25年05月14日 精选软件57个
[短剧] 2025年05月14日 精选+付费短剧推荐39部
[短剧] 2025年05月15日 精选+付费短剧推荐36部
- 最新评论
-
- 热门tag