How to have Measure-Object count number of lines correctly for files not having CRLF at the last line?

251 views Asked by At

In my understanding, (Get-Content $file | Measure-Object).count should return the total number of lines in the file. It does for some files, but for some generated files the result it gives is less than the correct number by 1. I examines them and see that at the last line they don't have CRLF.

Why is this the case? And how to make sure that I get the correct result? The Measure-Object documentation doesn't seem to explain this.

1

There are 1 answers

4
mklement0 On BEST ANSWER

The behavior is unrelated to Measure-Object:

It is Get-Content that reads and streams a given file's lines one by one, stripped of its trailing newline, if present on the last line.

That is, to Get-Content it doesn't make a difference whether the last line has a trailing newline, i.e. whether the file ends with a newline or not.

You need a different approach if you want to count the actual number of newlines (LF or CRLF sequences) in a file (and possibly add 1 if you want to consider the final newline a line in its own right), e.g.:

# Count the number of newlines
[regex]::Matches((Get-Content -Raw $file), '\n').Count

Alternatively:

((Get-Content -Raw $file) -replace '[^\n]').Length

Note the use of -Raw with Get-Content, which reads the file as a whole into a single, (typically) multiline string, thereby preserving all newlines.