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

[玩转系统] 在 PowerShell 中使用正则表达式从文件中选择字符串

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

在 PowerShell 中使用正则表达式从文件中选择字符串


[玩转系统] 在 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 参数来编写 $patternSelect-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 来选择 LineNumberLine 和每个具有匹配项的对象的 FileName 属性。

这就是 PowerShell 中使用正则表达式从文件中选择字符串的全部内容。

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

取消回复欢迎 发表评论:

关灯