8

我正在尝试在一个非常大的文本文件上运行以下命令。但是,它非常缓慢

((cat largefile.txt | select -first 1).split(",")).count()

是powershell中的另一种快速方法吗?无论如何,该命令似乎都会扫描整个文件。

4

2 回答 2

12

要仅获取文本文件中的前 x 行,请使用 –totalcount 参数:

((Get-Content largefile.txt -totalcount 1).split(",")).count
于 2012-07-06T21:32:46.403 回答
9

比这更糟糕 - 它会加载整个文件并将其转换为字符串数组。

使用本机 .NET 库仅加载第一行:

$reader = [System.IO.File]::OpenText("largefile.txt")
$line = $reader.ReadLine()
$reader.Close()

(借自How to process a file in Powershell line-by-line as a stream

于 2012-07-06T21:31:04.950 回答