Powershell Count行非常大的文件

use*_*975 3 powershell performance

我有一个非常大的文本文件,大小为250 GB,由供应商提供给我们.它们还为我们提供了一个控制文件,该文件应该包含大文件中的行数.有时会出现不匹配的情况.我如何计算Powershell中的线条?我尝试了这个命令,它运行了半个多小时,但还没有完成.

Get-content C:\test.txt | Measure-Object –Line

(gc C:\test.txt | Measure-object | select count).count
Run Code Online (Sandbox Code Playgroud)

任何帮助表示赞赏谢谢MR

dha*_*ech 11

文件

我有一个 1.39 GB 的 csv 文件:

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a----         10/4/2021   1:23 PM     1397998768 XBTUSD.csv
Run Code Online (Sandbox Code Playgroud)

WSL - 通过 NTFS

wc -l在 WSL 中。通过 访问文件/mnt/c/Users

速度较慢,因为文件位于 NTFS 端。

$ time wc -l XBTUSD.csv
41695261 XBTUSD.csv

real    0m10.935s
user    0m0.951s
sys     0m1.427s
Run Code Online (Sandbox Code Playgroud)

WSL - Linux 端的文件。

wc -l在 WSL 中。文件位于/tmp.

time wc -l /tmp/XBTUSD.csv
41695261 /tmp/XBTUSD.csv

real    0m0.447s
user    0m0.258s
sys     0m0.189s
Run Code Online (Sandbox Code Playgroud)

标准 PowerShell 方法

Measure-Command { Get-Content .\XBTUSD.csv | Measure-Object -Line }

Days              : 0
Hours             : 0
Minutes           : 7
Seconds           : 52
Milliseconds      : 353
Ticks             : 4723537381
TotalDays         : 0.00546705715393518
TotalHours        : 0.131209371694444
TotalMinutes      : 7.87256230166667
TotalSeconds      : 472.3537381
TotalMilliseconds : 472353.7381
Run Code Online (Sandbox Code Playgroud)

在 PowerShell 中使用 LINQ 方法:ReadLines以及Count

Measure-Command {
    [System.Linq.Enumerable]::Count(
        [System.IO.File]::ReadLines((ls .\XBTUSD.csv).FullName)) 
}
Run Code Online (Sandbox Code Playgroud)
Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 7
Milliseconds      : 263
Ticks             : 72636842
TotalDays         : 8.40704189814815E-05
TotalHours        : 0.00201769005555556
TotalMinutes      : 0.121061403333333
TotalSeconds      : 7.2636842
TotalMilliseconds : 7263.6842
Run Code Online (Sandbox Code Playgroud)

在 PowerShell 中切换

Measure-Command { 
    $count = 0; switch -File .\XBTUSD.csv { default { ++$count } }; $count 
}
Run Code Online (Sandbox Code Playgroud)
Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 44
Milliseconds      : 975
Ticks             : 449752555
TotalDays         : 0.000520546938657407
TotalHours        : 0.0124931265277778
TotalMinutes      : 0.749587591666667
TotalSeconds      : 44.9752555
TotalMilliseconds : 44975.2555
Run Code Online (Sandbox Code Playgroud)

概括

  • 最快的是wc -lWSL。

  • PowerShell 中最快的是 LINQ 方法


mkl*_*nt0 9

如果性能很重要,请避免使用cmdlet和管道; 用途switch -File:

$count = 0
switch -File C:\test.txt {
  default { ++$count }
}
Run Code Online (Sandbox Code Playgroud)

switch -File枚举指定文件的行; 条件default匹配任何行.


要了解性能差异:

# Create a sample file with 100,000 lines.
1..1e5 > tmp.txt
# Warm up the file cache
foreach ($line in [IO.File]::ReadLines("$pwd/tmp.txt")) { }

(Measure-Command { (Get-Content tmp.txt | Measure-Object).Count }).TotalSeconds

(Measure-Command { $count = 0; switch -File tmp.txt { default { ++$count } } }).TotalSeconds
Run Code Online (Sandbox Code Playgroud)

我的Windows 10/PSv5.1机器的示例结果:

1.3081307  # Get-Content + Measure-Object
0.1097513  # switch -File
Run Code Online (Sandbox Code Playgroud)

也就是说,在我的机器上,switch -File命令速度提高了大约12倍.

  • @Chipmunk_da,与“Get-Content”一样,“switch -File”在“CRLF”上分割行,但在“CR”和“LF”上也***分割行,因此这个答案不能在您的特定中使用情况。请参阅我对您原来问题的回答:[Powershell - Count number of returned line feed in .txt file](/sf/answers/4607044571/),了解可能适合您的内容。 (2认同)

Mik*_*Twc 6

对于如此大的文件,我宁愿使用一些 C 编写的实用程序。安装gitbash,它应该有wc命令:

wc -l yourfile.txt
Run Code Online (Sandbox Code Playgroud)

我在5GB/50M行文件(HDD上)上进行了测试,大约需要40s。最好的 powershell 解决方案大约需要 2 分钟。您还可以检查您的文件,它可能有一些自动增量索引或恒定的行大小。