PowerShell: how to count number of rows in csv file?

时光怂恿深爱的人放手 提交于 2019-11-28 06:53:54
Shay Levy

Pipe it to the Measure-Object cmdlet

Import-Csv C:\Directory\file.csv | Measure-Object
Ten98

Get-Content and Measure-Object are fine for small files, but both are super inefficient with memory. I had real problems with large files.

When counting rows in a 1GB file using either method, Powershell gobbled up all available memory on the server (8GB), then started paging to disk. I left it over an hour, but it was still paging to disk so I killed it.

The best method I found for large files is to use IO.StreamReader to load the file from disk and count each row using a variable. This keeps memory usage down to a very reasonable 25MB and is much, much quicker, taking around 30 seconds to count rows in a 1GB file or a couple of minutes for a 6GB file. It never eats up unreasonable amounts of RAM, no matter how large your file is:

[int]$LinesInFile = 0
$reader = New-Object IO.StreamReader 'c:\filename.csv'
 while($reader.ReadLine() -ne $null){ $LinesInFile++ }

The above snippet can be inserted wherever you would use get-content or measure-object, simply refer to the $LinesInFile variable to get the row count of the file.

stej

Generally (csv or not)

@(Get-Content c:\file.csv).Length

If the file has only one line, then, it will fail. (You need the @ prefix...otherwise if the file has one line, it will only count the number of characters in that line.

Get-Content c:\file.csv | Measure-Object -line

But both will fail if any record takes more than one row. Then better import csv and measure:

Import-Csv c:\file.csv | Measure-Object | Select-Object -expand count
julien

You can try

(Import-Csv C:\Directory\file.csv).count

or

$a=Import-Csv C:\Directory\file.csv
$a.count

(Import-Csv C:\Directory\file.csv).count is the only accurate one out of these.

I tried all of the other suggestions on a csv with 4781 rows, and all but this one returned 4803.

Sandeep Bharani

You can simply use unix like comand in powershell.

If you file test.csv Then command to get rowcount is

gc test.csv | Measure-Object
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!