Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell get number of lines of big (large) file

One of the ways to get number of lines from a file is this method in PowerShell:

PS C:\Users\Pranav\Desktop\PS_Test_Scripts> $a=Get-Content .\sub.ps1 PS C:\Users\Pranav\Desktop\PS_Test_Scripts> $a.count 34 PS C:\Users\Pranav\Desktop\PS_Test_Scripts>  

However, when I have a large 800 MB text file, how do I get the line number from it without reading the whole file?

The above method will consume too much RAM resulting in crashing the script or taking too long to complete.

like image 552
Pranav Avatar asked Aug 23 '12 04:08

Pranav


People also ask

How do I count the number of lines in a file 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 count the number of lines in a text file in Windows?

wc command - The wc (word count) command is one of the easiest and fastest methods of getting the amount of characters, lines, and words in a file.

How do I find the number of lines in a file?

The wc command is used to find the number of lines, characters, words, and bytes of a file. To find the number of lines using wc, we add the -l option. This will give us the total number of lines and the name of the file.

How do I count objects in PowerShell?

You can use Measure-Object to count objects or count objects with a specified Property. You can also use Measure-Object to calculate the Minimum, Maximum, Sum, StandardDeviation and Average of numeric values. For String objects, you can also use Measure-Object to count the number of lines, words, and characters.


1 Answers

Use Get-Content -Read $nLinesAtTime to read your file part by part:

$nlines = 0;  # Read file by 1000 lines at a time gc $YOURFILE -read 1000 | % { $nlines += $_.Length }; [string]::Format("{0} has {1} lines", $YOURFILE, $nlines) 

And here is simple, but slow script to validate work on a small file:

gc $YOURFILE | Measure-Object -Line 
like image 144
Akim Avatar answered Sep 25 '22 14:09

Akim