[玩转系统] 在 PowerShell 中使用正则表达式从文件中选择字符串
作者:精品下载站 日期:2024-12-14 05:28:09 浏览:12 分类:玩电脑
在 PowerShell 中使用正则表达式从文件中选择字符串
使用 Select-String
和正则表达式从文件中查找模式
在多种情况下,我们可以使用 Select-String
和正则表达式(也称为 regex)来从文件中搜索特定模式。其中一些如下:
- 在单个文件中搜索特定模式
- 在多个文件中搜索特定模式
- 从单个/多个文件中搜索多个模式
- 在目录及其子目录中的所有文件中查找特定模式
下面给出了我们将在本文中使用的文本文件。
.\file1.txt的内容:
This is a sample email [email protected].
We can also add some sample text plus additional emails.
Let's add 1 more as [email protected] and [email protected].
Now, we have 3 emails in this file.
.\file2.txt的内容:
Here are gmail users only:
First is the [email protected]
Second is the [email protected]
.\new\file3.txt 的内容:
Some products with available items:
We have 34 Product A in stock.
We have 34 Product B in stock.
下面让我们逐一了解一下。
在单个文件中搜索特定模式
使用 Select-String
cmdlet 和正则表达式从指定的单个文本文件中搜索特定模式。
将 Select-String 与 Regex 结合使用从文件中查找模式:
$pattern = "\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b"
Select-String -Path ".\file1.txt" -Pattern $pattern
输出 :
file.txt:1:This is a sample email [email protected].
file.txt:3:Let's add 1 more as [email protected] and [email protected].
首先,我们为试图在指定文件中查找的模式定义了一个正则表达式,并将其保存在 $pattern
变量中。在我们的例子中,我们搜索了有效的电子邮件地址。现在,重点是我们如何从给定文件中找到指定的模式。我们使用了 Select-String cmdlet,它用于从文件和字符串中查找文本或模式。
此 cmdlet 使用正则表达式在输入文件中搜索指定模式,这是使用 -Path
参数提及的。请注意,我们使用 -Pattern
参数来编写 $pattern
。 Select-String
基于文本行。默认情况下,它会在每一行中找到第一个匹配项,并且对于每个匹配项,它会打印文件名、行号以及具有匹配项的行中的所有整个文本(请参阅上面的输出并将其与 file.txt)。
我们还可以使用 Select-String 来查找每行的多个匹配项(稍后会了解)并在匹配项之前和之后显示文本。我们还可以显示一个布尔值 (True
/False
) 表示是否找到指定的匹配项。最后,如果我们想查看与给定模式不匹配的文本,我们也可以使用此 cmdlet。那么,如何使用呢?这完全取决于您的要求。以下是一些需要练习的场景。
在多个文件中搜索特定模式
使用 Select-String
cmdlet 和正则表达式从多个文本文件中搜索特定模式。
使用 Select-String 和正则表达式从多个文件中查找模式:
$pattern = "\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b"
Select-String -Path ".\file1.txt" -Pattern $pattern
Get-ChildItem "." -Filter "*.txt" | Select-String -Pattern $pattern
输出 :
file1.txt:1:This is a sample email [email protected].
file1.txt:3:Let's add 1 more as [email protected] and [email protected].
file2.txt:2:First is the [email protected]
file2.txt:3:Second is the [email protected]
首先,我们为保存在 $pattern
变量中的有效电子邮件地址定义了一个模式。然后,我们使用命令在当前目录中的所有文本文件中查找特定模式。让我们将该命令分成几块并理解它们。
我们使用Get-ChildItem
来获取当前目录中的项目和子项目(文件和目录),用点(.
)表示;您将完整路径写为C:\write\your\path\file。使用’-recurse’参数可以获取所有子容器中的所有项目。另一方面,如果您想限制递归的级别数,则可以使用 -Depth
参数。您可以在这里找到更多相关信息。
我们使用 -Filter
参数和 Get-ChildItem
来过滤所有项目和子项目,以仅接收 .txt
文件。然后,使用管道将这些文件传递给 Select-String
,我们在所有 .txt
文件中搜索指定的模式。
从单个/多个文件中搜索多个模式
使用 Select-String
cmdlet 和正则表达式从一个/多个文本文件中搜索多个模式。
将 Select-String 与 Regex 结合使用从一个文本文件中查找模式:
$pattern1 = "\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b"
$pattern2 = "\d+"
Select-String -Path ".\file1.txt" -Pattern $pattern1,$pattern2
输出 :
file1.txt:1:This is a sample email [email protected].
file1.txt:3:Let's add 1 more as [email protected] and [email protected].
file1.txt:4:Now, we have 3 emails in this file.
使用 Select-String 和正则表达式从多个文件中查找模式:
$pattern1 = "\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b"
$pattern2 = "\d+"
Get-ChildItem "." -Filter "*.txt" | Select-String -Pattern $pattern1,$pattern2
输出 :
file1.txt:1:This is a sample email [email protected].
file1.txt:3:Let's add 1 more as [email protected] and [email protected].
file1.txt:4:Now, we have 3 emails in this file.
file2.txt:2:First is the [email protected]
file2.txt:3:Second is the [email protected]
在前面的章节中,我们已经了解了 Select-String
、-Path
、-Pattern
、-Filter
和获取ChildItem
。我们还已经学习了如何在一个或多个文本文件中搜索特定模式。在这里,唯一的区别是我们定义了两种模式,一种是搜索有效电子邮件并将其保存在 $pattern1
中。第二个正则表达式是搜索一个或多个出现的数字并将其保存在 $pattern2
中。我们指定用逗号分隔的 $pattern1
和 $pattern2
(如上述脚本所示),以在一个或多个文本文件中搜索指定模式。
查找目录及其子目录中所有文件的模式
使用 Select-String
cmdlet 和正则表达式来搜索目录及其子目录中的所有文件。
使用 Select-String 和正则表达式在所有文件中搜索:
$pattern1 = "\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b"
$pattern2 = "\d+"
Get-ChildItem "." -Recurse | Select-String -Pattern $pattern1, $pattern2
输出 :
file1.txt:1:This is a sample email [email protected].
file1.txt:3:Let's add 1 more as [email protected] and [email protected].
file1.txt:4:Now, we have 3 emails in this file.
file2.txt:2:First is the [email protected]
file2.txt:3:Second is the [email protected]
new\file3.txt:2:We have 34 Product A in stock.
new\file3.txt:3:We have 34 Product B in stock.
上面的脚本与前面的代码示例类似,只是有一个区别;我们使用 -Recurse 参数来迭代指定目录及其子目录中的所有文件;您可以在上面的输出中观察到这一点。
我们只有 .txt
文件,但此脚本将迭代所有文件,无论其扩展名如何。如果您只想搜索.txt
文件,可以使用我们在从单个/多个文件中搜索多个模式
中学到的-Filter
参数部分。
现在,如果您不需要文件名而是匹配的行,那么您可以使用以下解决方案:我们将匹配的行保存在 $matches
变量中,并使用 访问该行.Line
属性。 .Line
将具有包含匹配模式的行文本。
仅获取线路:
$pattern1 = "\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b"
$pattern2 = "\d+"
$matches = Get-ChildItem "." -Recurse | Select-String -Pattern $pattern1, $pattern2
$matches.Line
输出 :
This is a sample email [email protected].
Let's add 1 more as [email protected] and [email protected].
Now, we have 3 emails in this file.
First is the [email protected]
Second is the [email protected]
We have 34 Product A in stock.
We have 34 Product B in stock.
我们还可以获得有组织的输出,包括包含匹配项的行号、文本和文件名。请参阅以下示例代码。
获取行号、文本和文件名:
$pattern1 = "\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b"
$pattern2 = "\d+"
Get-ChildItem "." -Recurse |
Select-String -Pattern $pattern1, $pattern2 |
Select-Object -Property LineNumber, Line, FileName
输出 :
LineNumber Line Filename
---------- ---- --------
1 This is a sample email [email protected]. file1.txt
3 Let's add 1 more as [email protected] and [email protected]. file1.txt
4 Now, we have 3 emails in this file. file1.txt
2 First is the [email protected] file2.txt
3 Second is the [email protected] file2.txt
2 We have 34 Product A in stock. file3.txt
3 We have 34 Product B in stock. file3.txt
在这里,我们使用带有 -Property
参数的 Select-Object
cmdlet 来选择 LineNumber
、Line
和每个具有匹配项的对象的 FileName
属性。
这就是 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