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

[玩转系统] 在 PowerShell 中按换行符拆分文本文件 [2 种方法]

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

在 PowerShell 中按换行符拆分文本文件 [2 种方法]


[玩转系统] 在 PowerShell 中按换行符拆分文本文件 [2 种方法]

使用 Get-Content Cmdlet

我们可以使用多种方式根据每个新行将指定的文本文件拆分为一个数组。为了练习这些方法和命令,我们将使用具有以下内容的 file.txt 文件。当然,您也可以创建或使用文本文件。

文件.txt的内容:

This is line one.
This is line two.
This is three and four on the same line. We didn't write it on a new line.
This is line five.

下面我们就继续学习一下解决方法。

使用 Get-Content cmdlet 根据每个新行将文本文件拆分为数组。

使用获取内容 Cmdlet:

$lines = Get-Content -Path .\file.txt
$lines.GetType()
$lines

输出 :

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Object[]                                 System.Array
This is line one.
This is line two.
This is three and four on the same line. We didn't write it on a new line.
This is line five.

我们使用 Get-Content cmdlet 来检索我们指定的文本文件的内容。使用此 cmdlet 是最直接的方法之一,因为默认情况下,它会将每一行文本作为字符串对象返回。结果,PowerShell对象的集合变成了字符串对象的数组。所以,我们不必分割它;它已由 Get-Content cmdlet 完成,我们将其保存在 $lines 变量中。

我们使用 -Path 参数来指定 Get-Content 将获取内容的项目路径。在我们的例子中,它是位于当前目录中的file.txt(用.表示);您也可以使用文件的完整路径。请记住,路径必须指向项目,而不是容器,这意味着我们可以提及一个或多个文件的路径,但不能提及目录。

为了确保我们成功地将文本文件分割成基于每个新行的数组,我们将 GetType() 方法与 $lines 变量链接起来。是的,我们已经做到了;如您所见,上面输出中的 BaseTypeSystem.Array。我们可以将特定索引的项目打印为 $lines[2];请注意,数组索引以 0 开始,以 n-1 结束,其中 n 是数组的大小。

现在想想,如果您有特定要求将 file.txt 读取为单个字符串,然后根据每个新行拆分它们,那么您必须使用 -Raw 参数作为在以下示例中进行了演示。您可以在这里找到其他有用的参数。

使用带有 -Raw 参数的 Get-Content Cmdlet:

$string = Get-Content -Path .\file.txt -Raw
$lines = $string -split "`n"
$lines.GetType()
$lines

输出 :

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Object[]                                 System.Array
This is line one.
This is line two.
This is three and four on the same line. We didn't write it on a new line.
This is line five.

我们使用 -Raw 参数忽略换行符,并将 file.txt 的整个数据作为一个字符串获取,并保留换行符,并将其保存在 $string 变量。接下来,我们使用 -Split 运算符并以换行符作为分隔符(定界符),根据每个换行符将文本文件拆分为数组。

同样,我们也可以使用 .Split() 方法,如下所示:

使用带有 -Raw 参数的 Get-Content Cmdlet:

$string = Get-Content -Path .\file.txt -Raw
$lines = $string.Split("`n")
$lines.GetType()
$lines

输出 :

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Object[]                                 System.Array
This is line one.
This is line two.
This is three and four on the same line. We didn't write it on a new line.
This is line five.

-Split.Split() 用于根据给定的分隔符将字符串拆分为子字符串数组。在 PowerShell 中,它们可以互换使用,但首选 -Split,因为它更短且易于键入。但是,如果您要根据复杂且具有挑战性的分隔符拆分字符串,最好使用 .Split() 方法。

-Raw 参数仅适用于文件系统驱动器。

使用 System.IO.File.ReadAllLines 方法

使用 System.IO.File.ReadAllLines() 方法根据每个新行将文本文件拆分为数组。

使用 .ReadAllLines() 方法:

$lines = [System.IO.File]::ReadAllLines("E:\Test\file.txt")
$lines.GetType()
$lines

输出 :

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     String[]                                 System.Array
This is line one.
This is line two.
This is three and four on the same line. We didn't write it on a new line.
This is line five.

这里,我们使用了File类的.ReadAllLines()方法;它也是一个单行解决方案,类似于我们在本文第一个示例中学习的 Get-Content cmdlet。 .ReadAllLines() 方法获取了 file.txt 的完整路径,读取所有行并将它们作为字符串数组返回,我们将其保存在 $lines 变量。

.ReadAllLines() 方法的好处是,它还会在读取所有内容后关闭文件,但基于您在此处找到的一些原因,您可能不得不面对一些异常。

这就是如何在 PowerShell 中使用换行符分割文本文件。

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

取消回复欢迎 发表评论:

关灯