簡體   English   中英

Powershell Count行非常大的文件

[英]Powershell Count lines extremely large file

我有一個非常大的文本文件,大小為 250 GB,由供應商提供給我們。 他們還為我們提供了一個控制文件,該文件應該包含大文件中的行數。 有時會出現不匹配。 如何計算 Powershell 中的行數? 我試過這個命令,它運行了半個多小時,還沒有完成。

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

(gc C:\test.txt | Measure-object | select count).count

任何幫助表示贊賞謝謝先生

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

$count = 0
switch -File C:\test.txt { default { ++$count } }

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

來自我的 Windows 10 / PSv5.1 機器的示例結果:

1.3081307  # Get-Content + Measure-Object
0.1097513  # switch -File

也就是說,在我的機器上, switch -File命令快了大約 12 倍。

對於這么大的文件,我寧願使用一些 C 編寫的實用程序。 安裝 gitbash,它應該有 wc 命令:

wc -l yourfile.txt

我在 5GB/50M 行文件(在 HDD 上)上對其進行了測試,大約需要 40 秒。 最好的 powershell 解決方案大約需要 2 分鍾。 您還可以檢查您的文件,它可能有一些自動增量索引或恆定的行大小。

文件

我有一個 1.39 GB 的 csv 文件:

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a----         10/4/2021   1:23 PM     1397998768 XBTUSD.csv

WSL - 通過 NTFS

WSL 中的wc -l 通過/mnt/c/Users訪問文件。

較慢,因為文件在 NTFS 端。

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

real    0m10.935s
user    0m0.951s
sys     0m1.427s

WSL - Linux 端的文件。

WSL 中的wc -l 文件在/tmp

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

real    0m0.447s
user    0m0.258s
sys     0m0.189s

標准 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

在 PowerShell 中使用 LINQ 方法: ReadLinesCount

Measure-Command {
    [System.Linq.Enumerable]::Count(
        [System.IO.File]::ReadLines((ls .\XBTUSD.csv).FullName)) 
}
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

在 PowerShell 中切換

Measure-Command { 
    $count = 0; switch -File .\XBTUSD.csv { default { ++$count } }; $count 
}
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

概括

  • 最快的是 WSL 中的wc -l

  • PowerShell 中最快的是 LINQ 方法

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM