Skip to content

Instantly share code, notes, and snippets.

@michaelvdnest
Created May 22, 2015 07:30
Show Gist options
  • Save michaelvdnest/09eae9906e9422297ae5 to your computer and use it in GitHub Desktop.
Save michaelvdnest/09eae9906e9422297ae5 to your computer and use it in GitHub Desktop.
Get lines from a file between two positions in a file.
Function Get-Lines {
<#
.SYNOPSIS
Get lines from a file.
.DESCRIPTION
Get lines from a file between two positions in a file.
You can return lines between a from line number until a to line number or
between regular expression patterns.
You can also specify a from line number to a pattern or from a pattern to
a line number.
If the pattern is not found nothing will be returned.
.PARAMETER Path
Mandatory. Specifies the path to the file to read.
.PARAMETER FromLine
If specified the lines will be read from this line number.
.PARAMETER ToLine
If specified the lines will be read to this line number.
.PARAMETER FromPattern
If specified the lines will be read from the line that matches this pattern.
.PARAMETER ToPattern
If specified the lines will be read to the line that matches this pattern.
.INPUTS
Parameters above
.OUTPUTS
System.String
.NOTES
Version: 1.0
Author: Michael van der Nest
Creation Date: 12/01/2015
Purpose/Change: Initial function development
.EXAMPLE
Get-Lines -Path "C:\Windows\Temp\Temp.txt" -FromLine 10 -ToLine 20
Description
-----------
This example displays the content between line 10 and 20 of the Temp.txt file.
.EXAMPLE
Get-Lines -Path "Y:\P.E150109.V3.20004518.ZA100094" "^25CSEVZAJJXXX0000000609232014" "^40000000609232014"
Description
-----------
This example displays the content between lines matching the from and to patterns.
#>
[CmdletBinding()]
Param (
[ValidateNotNullOrEmpty()]
[Parameter(Position=0,Mandatory=$true,ValueFromPipeline=$true)]
[ValidateScript({Test-Path $_})]
[Alias('FilePath')]
[string]
$Path,
[ValidateNotNullOrEmpty()]
[Parameter(Position=1,Mandatory=$true,ParameterSetName = "LineNumbers")]
[Parameter(Position=1,Mandatory=$true,ParameterSetName = "LineNum->Pattern")]
[int]
$FromLine,
[ValidateNotNullOrEmpty()]
[Parameter(Position=2,Mandatory=$true,ParameterSetName = "LineNumbers")]
[Parameter(Position=2,Mandatory=$true,ParameterSetName = "Pattern->LineNum")]
[int]
$ToLine,
[ValidateNotNullOrEmpty()]
[Parameter(Position=1,Mandatory=$true,ParameterSetName = "Patterns")]
[Parameter(Position=1,Mandatory=$true,ParameterSetName = "Pattern->LineNum")]
[string]
$FromPattern,
[ValidateNotNullOrEmpty()]
[Parameter(Position=2,Mandatory=$true,ParameterSetName = "Patterns")]
[Parameter(Position=2,Mandatory=$true,ParameterSetName = "LineNum->Pattern")]
[string]
$ToPattern
)
Begin {
Write-Verbose "$($MyInvocation.MyCommand.Name):: Function started with parameter set $($psCmdlet.ParameterSetName)"
switch ($psCmdlet.ParameterSetName) {
"LineNum->Pattern" {
Select-String -Path $Path -Pattern $ToPattern | % { $ToLine = $_.LineNumber }
break
}
"Pattern->LineNum" {
Select-String -Path $Path -Pattern $FromPattern | % { $FromLine = $_.LineNumber }
break
}
"Patterns" {
Select-String -Path $Path -Pattern $FromPattern | % { $FromLine = $_.LineNumber }
Select-String -Path $Path -Pattern $ToPattern | % { $ToLine = $_.LineNumber }
break
}
default {
break
}
}
if ($FromLine -eq $null) {
Write-Verbose "$($MyInvocation.MyCommand.Name):: From pattern not found in file."
$NoPatternFound = $true
}
if ($ToLine -eq $null) {
Write-Verbose "$($MyInvocation.MyCommand.Name):: To pattern not found in file."
$NoPatternFound = $true
}
if (-not $NoPatternFound) {
$reader = New-Object System.IO.StreamReader -Arg $Path
$count = 0
}
}
Process {
if ($NoPatternFound) { return }
if ($reader -eq $null) { return }
Write-Verbose "$($MyInvocation.MyCommand.Name):: Processing file: $Path"
while ($line = $reader.ReadLine()) {
$count++
if ($count -ge $FromLine -and $count -le $ToLine) {
Write-Output $line
}
elseif ($count -gt $ToLine) {
break
}
}
Write-Verbose "$($MyInvocation.MyCommand.Name):: Finished Processing file: $Path"
}
End {
$reader.Close()
Write-Verbose "$($MyInvocation.MyCommand.Name):: Function ended"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment