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.
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