Advertisement
Guest User

PowerShell Line Numbers in Console Function

a guest
Mar 8th, 2015
635
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#
  2. .SYNOPSIS
  3. Get-Lines gives you line numbers and has some of Get-Content's functionality among other things.
  4. I programmed it between 7th of March and 8th of March because I was compiling Java code within the
  5. PowerShell and I wanted to have a "Type MyClass.java -displayLineNumbers"
  6.    
  7. .NOTES
  8.     Script Version     : 1.0
  9.     Author             : Yannis K. Brand, Switzerland
  10.     Public Domain 2015 : Bitcoin Donation Address: 1NBc4SXBGDBAiokr1zdkJ6YaHyKdMHCKDG
  11.  
  12. .DESCRIPTION
  13.  
  14. I have written examples instead of a description.
  15.  
  16. *******************************
  17.  
  18.     Get-Help Get-Lines -Examples  |  more
  19.  
  20. *******************************    
  21.  
  22. Trying out this command really is the best help for it.
  23.  
  24. .EXAMPLE
  25. C:\PS>Get-Lines -Path C:\Work\MyText.txt
  26.  
  27. Description
  28.  
  29. -----------
  30. This command gets the content of the MyText.txt file and display each line preceeded with a line number.
  31.  
  32.  01 My Text would start here  
  33.  02 My Text would continue here  
  34.  03 My Text would go on here  
  35.  .
  36.  .
  37.  .
  38.  
  39. .EXAMPLE
  40. C:\PS>Get-Lines -Path C:\Work\MyText.txt -Random
  41.  
  42. Description
  43.  
  44. -----------
  45. This command gets a random line from MyText.txt and displays it with it's line number.
  46.  
  47.  09001 text at line 9001 (out of between 10000 and 99999 lines - notice the leading zero)
  48.  
  49.  
  50. .EXAMPLE
  51. C:\PS>Get-Lines -Path C:\Work\MyText.txt -Reverse
  52.  
  53. Description
  54.  
  55. -----------
  56.  
  57. Same as the first example but Lines are shown in reverse order.
  58.    
  59. .EXAMPLE
  60.  
  61. C:\PS>Get-Lines -Path C:\Work\MyText.txt -First 10
  62.  
  63. Description
  64.  
  65. -----------
  66.  
  67. Same but limited to first 10 lines.
  68.  
  69. .EXAMPLE
  70.  
  71. C:\PS>Get-Lines -Path C:\Work\MyText.txt -Last 10
  72.  
  73. Description
  74.  
  75. -----------
  76.  
  77. Last 10 lines are taken from MyText.txt and displayed ending with the last line.
  78.    
  79. .EXAMPLE
  80.  
  81. C:\PS>Get-Lines -Path C:\Work\MyText.txt -Last 10 -Reverse
  82.  
  83. Description
  84.  
  85. -----------
  86.  
  87. Last 10 lines are taken from MyText.txt and displayed starting with the last line.
  88.  
  89. .EXAMPLE
  90.  
  91. C:\PS>Get-Lines -Path C:\Work\MyText.txt -First 5 -Last 10
  92.  
  93. Description
  94.  
  95. -----------
  96.  
  97. This command uses the range from the fifth (5) line to the tenth (10) line.
  98.  
  99.  
  100. .EXAMPLE
  101.  
  102. C:\PS>Get-Lines -Path C:\Work\MyText.txt -First 5 -Last 10 -Reverse
  103.  
  104. Description
  105.  
  106. -----------
  107.  
  108. This command uses the range from the fifth (5) line to the tenth (10) line starting with the tenth line.
  109.  
  110. .LINK
  111. Feel free to donate Bitcoin:
  112. https://blockchain.info/address/1NBc4SXBGDBAiokr1zdkJ6YaHyKdMHCKDG
  113.    
  114. #>
  115. Function Get-Lines (){
  116.     Param
  117.     (
  118.      [parameter(Mandatory=$true)][String]$Path,
  119.      [parameter(Mandatory=$false)][int32]$First = [int32]::MaxValue,
  120.      [parameter(Mandatory=$false)][int32]$Last = [int32]::MaxValue,
  121.      [parameter(Mandatory=$false)][switch]$Reverse = $false,
  122.      [parameter(Mandatory=$false)][switch]$Random = $false
  123.     )
  124.     if($First -gt $Last -and $First -ne [int32]::MaxValue){        
  125.         $tmp = $First
  126.         $First = $Last
  127.         $Last = $tmp
  128.         write-host "Switched First and Last!"      
  129.     }
  130.     if($First -ile 0 -or $Last -ile 0){
  131.         $errorMessage = "No negative or zero values!"
  132.         Write-Error $errorMessage
  133.         return
  134.     }
  135.     if(!(Test-Path $Path)){
  136.         $errorMessage = "File $Path doesn't exist!"
  137.         Write-Error $errorMessage
  138.         return
  139.     }
  140.     $PathContent = Get-Content $Path
  141.     $lineCount = [int32](Get-Content $Path | Measure-Object -line).Lines
  142.     if($lineCount -eq 0){
  143.         write-host ""
  144.         return
  145.     }
  146.     if($First -ne [int32]::MaxValue -and $lineCount -lt $First){
  147.         $First = $lineCount
  148.     }
  149.     if($Last -ne [int32]::MaxValue -and $lineCount -lt $Last){
  150.         $Last = $lineCount
  151.     }
  152.     $lineCountLength = ($lineCount -as [String]).Length
  153.     if($Random -eq $true){
  154.         write-host ""
  155.         $lineNumber = Get-Random -Minimum 1 -Maximum ($lineCount + 1)  
  156.         Write-Host -NoNewline -BackgroundColor white " "
  157.         Write-Host -NoNewline -BackgroundColor white -ForegroundColor Blue $lineNumber
  158.         Write-Host -NoNewline -BackgroundColor white " "
  159.         Write-Host -NoNewline " "
  160.         Write-Output (Get-Content -Path $Path -First($lineNumber)).get($lineNumber - 1);
  161.         write-host ""
  162.         return
  163.     }
  164.     if($First -eq [int32]::MaxValue -and $Last -eq [int32]::MaxValue -and $reverse -eq $false){
  165.         write-host ""
  166.         $lineNumber = 1
  167.         $iexFormatHelper = "`"{0:D" + $lineCountLength + "}`" -f $lineNumber"
  168.         $lineCounter = Invoke-Expression -Command $iexFormatHelper
  169.         Write-Host -NoNewline -BackgroundColor white " "
  170.         Write-Host -NoNewline -BackgroundColor white -ForegroundColor Blue $lineCounter
  171.         Write-Host -NoNewline -BackgroundColor white " "
  172.         Write-Host -NoNewline " "
  173.         Write-Output (Get-Content -Path $Path -First($lineNumber));
  174.         for($lineNumber = 2; $lineNumber -ile $lineCount; $lineNumber++)
  175.         {
  176.             $iexFormatHelper = "`"{0:D" + $lineCountLength + "}`" -f $lineNumber"
  177.             $lineCounter = Invoke-Expression -Command $iexFormatHelper
  178.             Write-Host -NoNewline -BackgroundColor white " "
  179.             Write-Host -NoNewline -BackgroundColor white -ForegroundColor Blue $lineCounter
  180.             Write-Host -NoNewline -BackgroundColor white " "
  181.             Write-Host -NoNewline " "
  182.             Write-Output (Get-Content -Path $Path -TotalCount($lineNumber)).get($lineNumber - 1)
  183.         }
  184.         write-host ""
  185.         return
  186.     }
  187.     if(($First -eq [int32]::MaxValue -and $Last -eq [int32]::MaxValue) -and $Reverse){
  188.         write-host ""  
  189.         for($lineNumber = $lineCount; $lineNumber -gt 1; $lineNumber--)
  190.         {
  191.             $iexFormatHelper = "`"{0:D" + $lineCountLength + "}`" -f $lineNumber"
  192.             $lineCounter = Invoke-Expression -Command $iexFormatHelper
  193.             Write-Host -NoNewline -BackgroundColor white " "
  194.             Write-Host -NoNewline -BackgroundColor white -ForegroundColor Blue $lineCounter
  195.             Write-Host -NoNewline -BackgroundColor white " "
  196.             Write-Host -NoNewline " "
  197.             Write-Output (Get-Content -Path $Path -TotalCount($lineNumber)).get($lineNumber - 1)
  198.         }
  199.         $lineNumber = 1
  200.         $iexFormatHelper = "`"{0:D" + $lineCountLength + "}`" -f $lineNumber"
  201.         $lineCounter = Invoke-Expression -Command $iexFormatHelper
  202.         Write-Host -NoNewline -BackgroundColor white " "
  203.         Write-Host -NoNewline -BackgroundColor white -ForegroundColor Blue $lineCounter
  204.         Write-Host -NoNewline -BackgroundColor white " "
  205.         Write-Host -NoNewline " "
  206.         Write-Output (Get-Content -Path $Path -First($lineNumber));
  207.         write-host ""
  208.         return
  209.     }
  210.     if(($First -eq $Last -and $First -eq 1) -or ($First -eq 1 -and $Last -eq [int32]::MaxValue)){
  211.         write-host ""
  212.         $lineNumber = 1
  213.         Write-Host -NoNewline -BackgroundColor white " "
  214.         Write-Host -NoNewline -BackgroundColor white -ForegroundColor Blue $lineNumber
  215.         Write-Host -NoNewline -BackgroundColor white " "
  216.         Write-Host -NoNewline " "
  217.         Write-Output (Get-Content -Path $Path -First($lineNumber));
  218.         write-host ""
  219.         return
  220.     }
  221.     if($First -ne [int32]::MaxValue -and $Last -eq [int32]::MaxValue  -and $Reverse -eq $false){
  222.         write-host ""
  223.         $lineCount = $First
  224.         $lineNumber = 1
  225.         $iexFormatHelper = "`"{0:D" + $lineCountLength + "}`" -f $lineNumber"
  226.         $lineCounter = Invoke-Expression -Command $iexFormatHelper
  227.         Write-Host -NoNewline -BackgroundColor white " "
  228.         Write-Host -NoNewline -BackgroundColor white -ForegroundColor Blue $lineCounter
  229.         Write-Host -NoNewline -BackgroundColor white " "
  230.         Write-Host -NoNewline " "
  231.         Write-Output (Get-Content -Path $Path -First($lineNumber));
  232.         for($lineNumber = 2; $lineNumber -ile $lineCount; $lineNumber++)
  233.         {
  234.             $iexFormatHelper = "`"{0:D" + $lineCountLength + "}`" -f $lineNumber"
  235.             $lineCounter = Invoke-Expression -Command $iexFormatHelper
  236.             Write-Host -NoNewline -BackgroundColor white " "
  237.             Write-Host -NoNewline -BackgroundColor white -ForegroundColor Blue $lineCounter
  238.             Write-Host -NoNewline -BackgroundColor white " "
  239.             Write-Host -NoNewline " "
  240.             Write-Output (Get-Content -Path $Path -TotalCount($lineNumber)).get($lineNumber - 1)
  241.         }
  242.         write-host ""
  243.         return
  244.     }
  245.     if($First -ne [int32]::MaxValue -and $Last -eq [int32]::MaxValue -and $Reverse){
  246.         write-host ""
  247.         $lineCount = $First
  248.         for($lineNumber = $First; $lineNumber -gt 1; $lineNumber--)
  249.         {
  250.             $iexFormatHelper = "`"{0:D" + $lineCountLength + "}`" -f $lineNumber"
  251.             $lineCounter = Invoke-Expression -Command $iexFormatHelper
  252.             Write-Host -NoNewline -BackgroundColor white " "
  253.             Write-Host -NoNewline -BackgroundColor white -ForegroundColor Blue $lineCounter
  254.             Write-Host -NoNewline -BackgroundColor white "
  255.            "Write-Host -NoNewline " "
  256.             Write-Output (Get-Content -Path $Path -TotalCount($lineNumber)).get($lineNumber - 1)
  257.         }
  258.         $lineNumber = 1
  259.         $iexFormatHelper = "`"{0:D" + $lineCountLength + "}`" -f $lineNumber"
  260.         $lineCounter = Invoke-Expression -Command $iexFormatHelper
  261.         Write-Host -NoNewline -BackgroundColor white " "
  262.         Write-Host -NoNewline -BackgroundColor white -ForegroundColor Blue $lineCounter
  263.         Write-Host -NoNewline -BackgroundColor white " "
  264.         Write-Host -NoNewline " "
  265.         Write-Output (Get-Content -Path $Path -First($lineNumber));
  266.         write-host ""
  267.         return
  268.     }
  269.     if($First -eq $Last -and $First -gt 1 -and $First -ne [int32]::MaxValue){
  270.         write-host ""
  271.         $lineNumber = $First  
  272.         Write-Host -NoNewline -BackgroundColor white " "
  273.         Write-Host -NoNewline -BackgroundColor white -ForegroundColor Blue $lineNumber
  274.         Write-Host -NoNewline -BackgroundColor white " "
  275.         Write-Host -NoNewline " "
  276.         Write-Output (Get-Content -Path $Path -First($lineNumber)).get($lineNumber - 1);
  277.         write-host ""
  278.         return
  279.     }    
  280.     if($First -eq [int32]::MaxValue -and $Last -ne [int32]::MaxValue -and $Reverse){
  281.         write-host ""
  282.         for($lineNumber = $lineCount; $lineNumber -gt ($lineCount - $Last + 1); $lineNumber--)
  283.         {
  284.             $iexFormatHelper = "`"{0:D" + $lineCountLength + "}`" -f $lineNumber"
  285.             $lineCounter = Invoke-Expression -Command $iexFormatHelper
  286.             Write-Host -NoNewline -BackgroundColor white " "
  287.             Write-Host -NoNewline -BackgroundColor white -ForegroundColor Blue $lineCounter
  288.             Write-Host -NoNewline -BackgroundColor white " "
  289.             Write-Host -NoNewline " "
  290.             Write-Output (Get-Content -Path $Path -TotalCount($lineNumber)).get($lineNumber - 1)
  291.         }        
  292.         $lineNumber = $lineCount - $Last + 1
  293.         $iexFormatHelper = "`"{0:D" + $lineCountLength + "}`" -f $lineNumber"
  294.         $lineCounter = Invoke-Expression -Command $iexFormatHelper
  295.         Write-Host -NoNewline -BackgroundColor white " "
  296.         Write-Host -NoNewline -BackgroundColor white -ForegroundColor Blue $lineCounter
  297.         Write-Host -NoNewline -BackgroundColor white " "
  298.         Write-Host -NoNewline " "
  299.         if($Last -eq $lineCount){
  300.             Write-Output (Get-Content -Path $Path -First($lineNumber));            
  301.         }
  302.         else{
  303.             Write-Output (Get-Content -Path $Path -First($lineNumber)).get($lineNumber - 1);            
  304.         }
  305.         write-host ""
  306.         return
  307.     }
  308.     if($First -eq [int32]::MaxValue -and $Last -ne [uint32]::MaxValue){
  309.         write-host ""
  310.         $lineNumber = $lineCount - $Last + 1
  311.         $iexFormatHelper = "`"{0:D" + $lineCountLength + "}`" -f $lineNumber"
  312.         $lineCounter = Invoke-Expression -Command $iexFormatHelper
  313.         Write-Host -NoNewline -BackgroundColor white " "
  314.         Write-Host -NoNewline -BackgroundColor white -ForegroundColor Blue $lineCounter
  315.         Write-Host -NoNewline -BackgroundColor white " "
  316.         Write-Host -NoNewline " "
  317.         if($Last -eq $lineCount){
  318.             Write-Output (Get-Content -Path $Path -First($lineNumber));            
  319.         }
  320.         else{
  321.             Write-Output (Get-Content -Path $Path -First($lineNumber)).get($lineNumber - 1);            
  322.         }
  323.         for($lineNumber = $lineCount - $Last + 2; $lineNumber -lt $lineCount + 1; $lineNumber++)
  324.         {
  325.             $iexFormatHelper = "`"{0:D" + $lineCountLength + "}`" -f $lineNumber"
  326.             $lineCounter = Invoke-Expression -Command $iexFormatHelper
  327.             Write-Host -NoNewline -BackgroundColor white " "
  328.             Write-Host -NoNewline -BackgroundColor white -ForegroundColor Blue $lineCounter
  329.             Write-Host -NoNewline -BackgroundColor white " "
  330.             Write-Host -NoNewline " "
  331.             Write-Output (Get-Content -Path $Path -TotalCount($lineNumber)).get($lineNumber - 1)
  332.         }        
  333.         write-host ""
  334.         return
  335.     }
  336.     if($First -lt $Last -and $Reverse -eq $false){
  337.         write-host ""
  338.         $lineNumber = $First
  339.         $iexFormatHelper = "`"{0:D" + $lineCountLength + "}`" -f $lineNumber"
  340.         $lineCounter = Invoke-Expression -Command $iexFormatHelper
  341.         Write-Host -NoNewline -BackgroundColor white " "
  342.         Write-Host -NoNewline -BackgroundColor white -ForegroundColor Blue $lineCounter
  343.         Write-Host -NoNewline -BackgroundColor white " "
  344.         Write-Host -NoNewline " "
  345.         if($First -eq 1){
  346.             Write-Output (Get-Content -Path $Path -First($lineNumber));
  347.         }
  348.         else{
  349.             Write-Output (Get-Content -Path $Path -First($lineNumber)).get($lineNumber - 1);
  350.         }
  351.         for($lineNumber = $First + 1; $lineNumber -ile $Last; $lineNumber++)
  352.         {
  353.             $iexFormatHelper = "`"{0:D" + $lineCountLength + "}`" -f $lineNumber"
  354.             $lineCounter = Invoke-Expression -Command $iexFormatHelper
  355.             Write-Host -NoNewline -BackgroundColor white " "
  356.             Write-Host -NoNewline -BackgroundColor white -ForegroundColor Blue $lineCounter
  357.             Write-Host -NoNewline -BackgroundColor white " "
  358.             Write-Host -NoNewline " "
  359.             Write-Output (Get-Content -Path $Path -TotalCount($lineNumber)).get($lineNumber - 1)
  360.         }
  361.         write-host ""
  362.         return
  363.     }
  364.     if($First -lt $Last -and $Reverse){
  365.         write-host ""
  366.         for($lineNumber = $Last; $lineNumber -gt $First; $lineNumber--)
  367.         {
  368.             $iexFormatHelper = "`"{0:D" + $lineCountLength + "}`" -f $lineNumber"
  369.             $lineCounter = Invoke-Expression -Command $iexFormatHelper
  370.             Write-Host -NoNewline -BackgroundColor white " "
  371.             Write-Host -NoNewline -BackgroundColor white -ForegroundColor Blue $lineCounter
  372.             Write-Host -NoNewline -BackgroundColor white " "
  373.             Write-Host -NoNewline " "
  374.             Write-Output (Get-Content -Path $Path -TotalCount($lineNumber)).get($lineNumber - 1)
  375.         }
  376.         $lineNumber = $First
  377.         $iexFormatHelper = "`"{0:D" + $lineCountLength + "}`" -f $lineNumber"
  378.         $lineCounter = Invoke-Expression -Command $iexFormatHelper
  379.         Write-Host -NoNewline -BackgroundColor white " "
  380.         Write-Host -NoNewline -BackgroundColor white -ForegroundColor Blue $lineCounter
  381.         Write-Host -NoNewline -BackgroundColor white " "
  382.         Write-Host -NoNewline " "
  383.         if($First -eq 1){
  384.             Write-Output (Get-Content -Path $Path -First($lineNumber));
  385.         }
  386.         else{
  387.             Write-Output (Get-Content -Path $Path -First($lineNumber)).get($lineNumber - 1);
  388.         }    
  389.         write-host ""
  390.         return
  391.     }
  392. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement