Reputation: 1858
How do I get the number of lines before the blank line. For example:
This is a paragraph This is a paragraph This is a paragraph This is a paragraph This is a paragraph This is a paragraph This is a paragraph
The first set should count 3 lines and the second set counts 4 lines.
I have used Get-Content .\testing.txt | Measure-Object -line
which returns the total number of line but that's not what I wanted.
Upvotes: 2
Views: 1795
Reputation: 293
Get-Content
returns an array so any carriage return or linefeed are lost. Assuming there are no spaces the following returns 3. Adjust accordingly to trim "spaces" if they are not considered characters.
$a = Get-Content .\testing.txt ; [array]::IndexOf($a,'')
Edit: The following loops the Content and outputs the number of lines preceding any blank line. An empty line or a line with any number of spaces is considered blank. To treat spaces as not a blank line, remove ".trimend".
$a = Get-Content C:\powershell\tmp.txt ; $i = 0;
while( [array]::IndexOf($a.trimend(' '),'', $i) -gt 0)
{
$j = [array]::IndexOf($a.trimend(' '),'', $i); $j - $i; $i = $j + 1 ;
}
Upvotes: 3
Reputation: 13179
This would be stepping through the lines of the file to find first blank.
$lines = Get-Content C:\Dev\PS\3.txt
for ($i = 0; $i -le $lines.Length; $i++) {
if ($lines[$i].Length -eq 0) {
break
}
}
echo $i
Upvotes: 1