【问题标题】:Powershell Count lines extremely large filePowershell Count行非常大的文件
【发布时间】:2019-02-26 20:08:33
【问题描述】:

我有一个供应商提供给我们的 250 GB 的超大文本文件。他们还为我们提供了一个控制文件,该文件应该包含大文件中的行数。有时会出现不匹配。 如何在 Powershell 中计算行数?这个命令我试过了,跑了半个多小时还没完成。

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

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

感谢任何帮助 谢谢 先生

【问题讨论】:

    标签: performance powershell


    【解决方案1】:

    文件

    我有一个 1.39 GB 的 csv 文件:

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

    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
    

    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
    

    标准 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 方法

    【讨论】:

      【解决方案2】:

      如果性能很重要,请避免使用 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 倍。

      【讨论】:

      • 这个可以用来统计大文件中CRLF的个数吗?我在这里问了一个问题并收到了一个解决方案,该解决方案适用于较小的 .txt 文件,但对于大文件来说内存不足 - stackoverflow.com/q/65813430/6900402
      • @Chipmunk_da,与Get-Content 一样,switch -FileCRLF 上拆分行,但在CRLF ,因此此答案不能用于您的具体情况。请参阅我对您最初问题的回答:Powershell - Count number of carriage returns line feed in .txt file 了解可能对您有用的内容。
      【解决方案3】:

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

      wc -l yourfile.txt
      

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

      【讨论】:

        猜你喜欢
        • 2018-06-14
        • 1970-01-01
        • 2017-11-18
        • 2014-10-22
        • 2013-05-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-11-05
        相关资源
        最近更新 更多