Scenario: I want only line 1053 from a large file. This might be a line flagged by a debugger, malware detector, or script analyzer.
There are many ways to do this, but some are very inefficient. It would be great to enclose a best-performance method in a parameter.
Get-Content -Line
When the line doesn't exist in the file, Get-Content returns nothing (no error).
Good idea. What if you wanted to obtain a range of lines, or an array of individual lines?
### Get an individual line
Get-Content -Path ... -Line 1053
### Get a range of lines
Get-Content -Path ... -Line 1053-1058
### Get more than one individual line
Get-Content -Path ... -Line 1053,2130
### Get line ranges and individual lines
Get-Content -Path ... -Line 1053-1058, 2130
I suppose you could treat a single line as a range with a length of one.
Nice add @pcgeek86 but remember the range operator is ..
so Line would be of type [int[]]
and take -Line 1053
, -Line 1053..1058
and -Line (1053..1058),2130
.
When the line doesn't exist in the file, Get-Content returns nothing (no error).
I believe this should cause an exception. If we do not want it, then -ErrorAction SilentlyContinue
Well, at the most I would say a missing line would cause a non-terminating error.
An issue to think about is whether -Line
specifies 0 or 1 based indices. I think 1-based is the way to go since most tools report line errors/warnings using 1-based indices.
Error reporting is not the only scenario and perhaps it is better to use a general rule - start with 0.
Hmm "Get-Content -Path ... -Line 1053" instead of (Get-Content -Path ...)[1053] doesn't seem like much of a win to me (maybe for very large files...). Also remember that Get-Content is a provider cmdlet so is -Line a dynamic parameter for the file system provider? Or does it apply to all providers. Finally (ignoring pipeline performance issues)
get-content -path ... | select -range 1053,1055
is more "powershelly".
Also note that you can also do
Get-Content -Path ... | Select -Skip 1052 -First 1
Most likely a dynamic param on FileSystemProvider. @BrucePay: I agree that your examples are more powershelly, but it is also nice to have scripts that doesn't only work on small files (where work may be "work reasonably fast").
(Get-Content -Path ...)[1053] may be really bad.
I don't have a strong opinion about how to solve this, but in general, I would like to see a bit more focus on making our core cmdlets performant even on very large data sets.
I find myself starting with pipelines for interactively trying things out, but quite often moving to other solutions when making a module out of my findings, often for performance reasons. And as with most perf opt, the result is less readable.
If this can be done internally, this should be done internally for performance reasons.
Most helpful comment
Most likely a dynamic param on FileSystemProvider. @BrucePay: I agree that your examples are more powershelly, but it is also nice to have scripts that doesn't only work on small files (where work may be "work reasonably fast").
(Get-Content -Path ...)[1053] may be really bad.
I don't have a strong opinion about how to solve this, but in general, I would like to see a bit more focus on making our core cmdlets performant even on very large data sets.
I find myself starting with pipelines for interactively trying things out, but quite often moving to other solutions when making a module out of my findings, often for performance reasons. And as with most perf opt, the result is less readable.