Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

How can I count the number of rows in a csv file using powershell? I tried something like

Get-Content -length "C:\Directory\file.csv" 

or

(Get-Content).length "C:\Directory\file.csv" 

but these result an error.

like image 554
jrara Avatar asked Jul 28 '11 08:07

jrara


People also ask

How do I count the number of rows in a csv file?

Using len() function Under this method, we need to read the CSV file using pandas library and then use the len() function with the imported CSV file, which will return an int value of a number of lines/rows present in the CSV file.

How do I count rows in PowerShell?

To count the total number of lines in the file in PowerShell, you first need to retrieve the content of the item using Get-Content cmdlet and need to use method Length() to retrieve the total number of lines.

How do I find the number of rows in a csv file in Linux?

The wc command is used to count the number of words or lines (with the -l option) in a file. In a CSV file, each line is a data record. Counting the number of rows is an easy way to have the number of records in your CSV file.


1 Answers

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.

like image 131
Ten98 Avatar answered Sep 19 '22 22:09

Ten98