Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <#
- .SYNOPSIS
- Get-Lines gives you line numbers and has some of Get-Content's functionality among other things.
- I programmed it between 7th of March and 8th of March because I was compiling Java code within the
- PowerShell and I wanted to have a "Type MyClass.java -displayLineNumbers"
- .NOTES
- Script Version : 1.0
- Author : Yannis K. Brand, Switzerland
- Public Domain 2015 : Bitcoin Donation Address: 1NBc4SXBGDBAiokr1zdkJ6YaHyKdMHCKDG
- .DESCRIPTION
- I have written examples instead of a description.
- *******************************
- Get-Help Get-Lines -Examples | more
- *******************************
- Trying out this command really is the best help for it.
- .EXAMPLE
- C:\PS>Get-Lines -Path C:\Work\MyText.txt
- Description
- -----------
- This command gets the content of the MyText.txt file and display each line preceeded with a line number.
- 01 My Text would start here
- 02 My Text would continue here
- 03 My Text would go on here
- .
- .
- .
- .EXAMPLE
- C:\PS>Get-Lines -Path C:\Work\MyText.txt -Random
- Description
- -----------
- This command gets a random line from MyText.txt and displays it with it's line number.
- 09001 text at line 9001 (out of between 10000 and 99999 lines - notice the leading zero)
- .EXAMPLE
- C:\PS>Get-Lines -Path C:\Work\MyText.txt -Reverse
- Description
- -----------
- Same as the first example but Lines are shown in reverse order.
- .EXAMPLE
- C:\PS>Get-Lines -Path C:\Work\MyText.txt -First 10
- Description
- -----------
- Same but limited to first 10 lines.
- .EXAMPLE
- C:\PS>Get-Lines -Path C:\Work\MyText.txt -Last 10
- Description
- -----------
- Last 10 lines are taken from MyText.txt and displayed ending with the last line.
- .EXAMPLE
- C:\PS>Get-Lines -Path C:\Work\MyText.txt -Last 10 -Reverse
- Description
- -----------
- Last 10 lines are taken from MyText.txt and displayed starting with the last line.
- .EXAMPLE
- C:\PS>Get-Lines -Path C:\Work\MyText.txt -First 5 -Last 10
- Description
- -----------
- This command uses the range from the fifth (5) line to the tenth (10) line.
- .EXAMPLE
- C:\PS>Get-Lines -Path C:\Work\MyText.txt -First 5 -Last 10 -Reverse
- Description
- -----------
- This command uses the range from the fifth (5) line to the tenth (10) line starting with the tenth line.
- .LINK
- Feel free to donate Bitcoin:
- https://blockchain.info/address/1NBc4SXBGDBAiokr1zdkJ6YaHyKdMHCKDG
- #>
- Function Get-Lines (){
- Param
- (
- [parameter(Mandatory=$true)][String]$Path,
- [parameter(Mandatory=$false)][int32]$First = [int32]::MaxValue,
- [parameter(Mandatory=$false)][int32]$Last = [int32]::MaxValue,
- [parameter(Mandatory=$false)][switch]$Reverse = $false,
- [parameter(Mandatory=$false)][switch]$Random = $false
- )
- if($First -gt $Last -and $First -ne [int32]::MaxValue){
- $tmp = $First
- $First = $Last
- $Last = $tmp
- write-host "Switched First and Last!"
- }
- if($First -ile 0 -or $Last -ile 0){
- $errorMessage = "No negative or zero values!"
- Write-Error $errorMessage
- return
- }
- if(!(Test-Path $Path)){
- $errorMessage = "File $Path doesn't exist!"
- Write-Error $errorMessage
- return
- }
- $PathContent = Get-Content $Path
- $lineCount = [int32](Get-Content $Path | Measure-Object -line).Lines
- if($lineCount -eq 0){
- write-host ""
- return
- }
- if($First -ne [int32]::MaxValue -and $lineCount -lt $First){
- $First = $lineCount
- }
- if($Last -ne [int32]::MaxValue -and $lineCount -lt $Last){
- $Last = $lineCount
- }
- $lineCountLength = ($lineCount -as [String]).Length
- if($Random -eq $true){
- write-host ""
- $lineNumber = Get-Random -Minimum 1 -Maximum ($lineCount + 1)
- Write-Host -NoNewline -BackgroundColor white " "
- Write-Host -NoNewline -BackgroundColor white -ForegroundColor Blue $lineNumber
- Write-Host -NoNewline -BackgroundColor white " "
- Write-Host -NoNewline " "
- Write-Output (Get-Content -Path $Path -First($lineNumber)).get($lineNumber - 1);
- write-host ""
- return
- }
- if($First -eq [int32]::MaxValue -and $Last -eq [int32]::MaxValue -and $reverse -eq $false){
- write-host ""
- $lineNumber = 1
- $iexFormatHelper = "`"{0:D" + $lineCountLength + "}`" -f $lineNumber"
- $lineCounter = Invoke-Expression -Command $iexFormatHelper
- Write-Host -NoNewline -BackgroundColor white " "
- Write-Host -NoNewline -BackgroundColor white -ForegroundColor Blue $lineCounter
- Write-Host -NoNewline -BackgroundColor white " "
- Write-Host -NoNewline " "
- Write-Output (Get-Content -Path $Path -First($lineNumber));
- for($lineNumber = 2; $lineNumber -ile $lineCount; $lineNumber++)
- {
- $iexFormatHelper = "`"{0:D" + $lineCountLength + "}`" -f $lineNumber"
- $lineCounter = Invoke-Expression -Command $iexFormatHelper
- Write-Host -NoNewline -BackgroundColor white " "
- Write-Host -NoNewline -BackgroundColor white -ForegroundColor Blue $lineCounter
- Write-Host -NoNewline -BackgroundColor white " "
- Write-Host -NoNewline " "
- Write-Output (Get-Content -Path $Path -TotalCount($lineNumber)).get($lineNumber - 1)
- }
- write-host ""
- return
- }
- if(($First -eq [int32]::MaxValue -and $Last -eq [int32]::MaxValue) -and $Reverse){
- write-host ""
- for($lineNumber = $lineCount; $lineNumber -gt 1; $lineNumber--)
- {
- $iexFormatHelper = "`"{0:D" + $lineCountLength + "}`" -f $lineNumber"
- $lineCounter = Invoke-Expression -Command $iexFormatHelper
- Write-Host -NoNewline -BackgroundColor white " "
- Write-Host -NoNewline -BackgroundColor white -ForegroundColor Blue $lineCounter
- Write-Host -NoNewline -BackgroundColor white " "
- Write-Host -NoNewline " "
- Write-Output (Get-Content -Path $Path -TotalCount($lineNumber)).get($lineNumber - 1)
- }
- $lineNumber = 1
- $iexFormatHelper = "`"{0:D" + $lineCountLength + "}`" -f $lineNumber"
- $lineCounter = Invoke-Expression -Command $iexFormatHelper
- Write-Host -NoNewline -BackgroundColor white " "
- Write-Host -NoNewline -BackgroundColor white -ForegroundColor Blue $lineCounter
- Write-Host -NoNewline -BackgroundColor white " "
- Write-Host -NoNewline " "
- Write-Output (Get-Content -Path $Path -First($lineNumber));
- write-host ""
- return
- }
- if(($First -eq $Last -and $First -eq 1) -or ($First -eq 1 -and $Last -eq [int32]::MaxValue)){
- write-host ""
- $lineNumber = 1
- Write-Host -NoNewline -BackgroundColor white " "
- Write-Host -NoNewline -BackgroundColor white -ForegroundColor Blue $lineNumber
- Write-Host -NoNewline -BackgroundColor white " "
- Write-Host -NoNewline " "
- Write-Output (Get-Content -Path $Path -First($lineNumber));
- write-host ""
- return
- }
- if($First -ne [int32]::MaxValue -and $Last -eq [int32]::MaxValue -and $Reverse -eq $false){
- write-host ""
- $lineCount = $First
- $lineNumber = 1
- $iexFormatHelper = "`"{0:D" + $lineCountLength + "}`" -f $lineNumber"
- $lineCounter = Invoke-Expression -Command $iexFormatHelper
- Write-Host -NoNewline -BackgroundColor white " "
- Write-Host -NoNewline -BackgroundColor white -ForegroundColor Blue $lineCounter
- Write-Host -NoNewline -BackgroundColor white " "
- Write-Host -NoNewline " "
- Write-Output (Get-Content -Path $Path -First($lineNumber));
- for($lineNumber = 2; $lineNumber -ile $lineCount; $lineNumber++)
- {
- $iexFormatHelper = "`"{0:D" + $lineCountLength + "}`" -f $lineNumber"
- $lineCounter = Invoke-Expression -Command $iexFormatHelper
- Write-Host -NoNewline -BackgroundColor white " "
- Write-Host -NoNewline -BackgroundColor white -ForegroundColor Blue $lineCounter
- Write-Host -NoNewline -BackgroundColor white " "
- Write-Host -NoNewline " "
- Write-Output (Get-Content -Path $Path -TotalCount($lineNumber)).get($lineNumber - 1)
- }
- write-host ""
- return
- }
- if($First -ne [int32]::MaxValue -and $Last -eq [int32]::MaxValue -and $Reverse){
- write-host ""
- $lineCount = $First
- for($lineNumber = $First; $lineNumber -gt 1; $lineNumber--)
- {
- $iexFormatHelper = "`"{0:D" + $lineCountLength + "}`" -f $lineNumber"
- $lineCounter = Invoke-Expression -Command $iexFormatHelper
- Write-Host -NoNewline -BackgroundColor white " "
- Write-Host -NoNewline -BackgroundColor white -ForegroundColor Blue $lineCounter
- Write-Host -NoNewline -BackgroundColor white "
- "Write-Host -NoNewline " "
- Write-Output (Get-Content -Path $Path -TotalCount($lineNumber)).get($lineNumber - 1)
- }
- $lineNumber = 1
- $iexFormatHelper = "`"{0:D" + $lineCountLength + "}`" -f $lineNumber"
- $lineCounter = Invoke-Expression -Command $iexFormatHelper
- Write-Host -NoNewline -BackgroundColor white " "
- Write-Host -NoNewline -BackgroundColor white -ForegroundColor Blue $lineCounter
- Write-Host -NoNewline -BackgroundColor white " "
- Write-Host -NoNewline " "
- Write-Output (Get-Content -Path $Path -First($lineNumber));
- write-host ""
- return
- }
- if($First -eq $Last -and $First -gt 1 -and $First -ne [int32]::MaxValue){
- write-host ""
- $lineNumber = $First
- Write-Host -NoNewline -BackgroundColor white " "
- Write-Host -NoNewline -BackgroundColor white -ForegroundColor Blue $lineNumber
- Write-Host -NoNewline -BackgroundColor white " "
- Write-Host -NoNewline " "
- Write-Output (Get-Content -Path $Path -First($lineNumber)).get($lineNumber - 1);
- write-host ""
- return
- }
- if($First -eq [int32]::MaxValue -and $Last -ne [int32]::MaxValue -and $Reverse){
- write-host ""
- for($lineNumber = $lineCount; $lineNumber -gt ($lineCount - $Last + 1); $lineNumber--)
- {
- $iexFormatHelper = "`"{0:D" + $lineCountLength + "}`" -f $lineNumber"
- $lineCounter = Invoke-Expression -Command $iexFormatHelper
- Write-Host -NoNewline -BackgroundColor white " "
- Write-Host -NoNewline -BackgroundColor white -ForegroundColor Blue $lineCounter
- Write-Host -NoNewline -BackgroundColor white " "
- Write-Host -NoNewline " "
- Write-Output (Get-Content -Path $Path -TotalCount($lineNumber)).get($lineNumber - 1)
- }
- $lineNumber = $lineCount - $Last + 1
- $iexFormatHelper = "`"{0:D" + $lineCountLength + "}`" -f $lineNumber"
- $lineCounter = Invoke-Expression -Command $iexFormatHelper
- Write-Host -NoNewline -BackgroundColor white " "
- Write-Host -NoNewline -BackgroundColor white -ForegroundColor Blue $lineCounter
- Write-Host -NoNewline -BackgroundColor white " "
- Write-Host -NoNewline " "
- if($Last -eq $lineCount){
- Write-Output (Get-Content -Path $Path -First($lineNumber));
- }
- else{
- Write-Output (Get-Content -Path $Path -First($lineNumber)).get($lineNumber - 1);
- }
- write-host ""
- return
- }
- if($First -eq [int32]::MaxValue -and $Last -ne [uint32]::MaxValue){
- write-host ""
- $lineNumber = $lineCount - $Last + 1
- $iexFormatHelper = "`"{0:D" + $lineCountLength + "}`" -f $lineNumber"
- $lineCounter = Invoke-Expression -Command $iexFormatHelper
- Write-Host -NoNewline -BackgroundColor white " "
- Write-Host -NoNewline -BackgroundColor white -ForegroundColor Blue $lineCounter
- Write-Host -NoNewline -BackgroundColor white " "
- Write-Host -NoNewline " "
- if($Last -eq $lineCount){
- Write-Output (Get-Content -Path $Path -First($lineNumber));
- }
- else{
- Write-Output (Get-Content -Path $Path -First($lineNumber)).get($lineNumber - 1);
- }
- for($lineNumber = $lineCount - $Last + 2; $lineNumber -lt $lineCount + 1; $lineNumber++)
- {
- $iexFormatHelper = "`"{0:D" + $lineCountLength + "}`" -f $lineNumber"
- $lineCounter = Invoke-Expression -Command $iexFormatHelper
- Write-Host -NoNewline -BackgroundColor white " "
- Write-Host -NoNewline -BackgroundColor white -ForegroundColor Blue $lineCounter
- Write-Host -NoNewline -BackgroundColor white " "
- Write-Host -NoNewline " "
- Write-Output (Get-Content -Path $Path -TotalCount($lineNumber)).get($lineNumber - 1)
- }
- write-host ""
- return
- }
- if($First -lt $Last -and $Reverse -eq $false){
- write-host ""
- $lineNumber = $First
- $iexFormatHelper = "`"{0:D" + $lineCountLength + "}`" -f $lineNumber"
- $lineCounter = Invoke-Expression -Command $iexFormatHelper
- Write-Host -NoNewline -BackgroundColor white " "
- Write-Host -NoNewline -BackgroundColor white -ForegroundColor Blue $lineCounter
- Write-Host -NoNewline -BackgroundColor white " "
- Write-Host -NoNewline " "
- if($First -eq 1){
- Write-Output (Get-Content -Path $Path -First($lineNumber));
- }
- else{
- Write-Output (Get-Content -Path $Path -First($lineNumber)).get($lineNumber - 1);
- }
- for($lineNumber = $First + 1; $lineNumber -ile $Last; $lineNumber++)
- {
- $iexFormatHelper = "`"{0:D" + $lineCountLength + "}`" -f $lineNumber"
- $lineCounter = Invoke-Expression -Command $iexFormatHelper
- Write-Host -NoNewline -BackgroundColor white " "
- Write-Host -NoNewline -BackgroundColor white -ForegroundColor Blue $lineCounter
- Write-Host -NoNewline -BackgroundColor white " "
- Write-Host -NoNewline " "
- Write-Output (Get-Content -Path $Path -TotalCount($lineNumber)).get($lineNumber - 1)
- }
- write-host ""
- return
- }
- if($First -lt $Last -and $Reverse){
- write-host ""
- for($lineNumber = $Last; $lineNumber -gt $First; $lineNumber--)
- {
- $iexFormatHelper = "`"{0:D" + $lineCountLength + "}`" -f $lineNumber"
- $lineCounter = Invoke-Expression -Command $iexFormatHelper
- Write-Host -NoNewline -BackgroundColor white " "
- Write-Host -NoNewline -BackgroundColor white -ForegroundColor Blue $lineCounter
- Write-Host -NoNewline -BackgroundColor white " "
- Write-Host -NoNewline " "
- Write-Output (Get-Content -Path $Path -TotalCount($lineNumber)).get($lineNumber - 1)
- }
- $lineNumber = $First
- $iexFormatHelper = "`"{0:D" + $lineCountLength + "}`" -f $lineNumber"
- $lineCounter = Invoke-Expression -Command $iexFormatHelper
- Write-Host -NoNewline -BackgroundColor white " "
- Write-Host -NoNewline -BackgroundColor white -ForegroundColor Blue $lineCounter
- Write-Host -NoNewline -BackgroundColor white " "
- Write-Host -NoNewline " "
- if($First -eq 1){
- Write-Output (Get-Content -Path $Path -First($lineNumber));
- }
- else{
- Write-Output (Get-Content -Path $Path -First($lineNumber)).get($lineNumber - 1);
- }
- write-host ""
- return
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement