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.
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With