question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Could Select-String -List be aliased to Select-String -First [$Num] ?

See original GitHub issue

I was looking to stop searching a file after the first match, expecting Select-String -First 1.

Select-String | Select-Object -First 1 can stop the pipeline, and skip searching the rest of the file. This is fine, but later I found there is a parameter to Select-String which stops after the first match, and outputs the match information, it just has the not-obvious name -List (where did that come from?).

It is the only cmdlet in Core, Management, Utility modules with a -List parameter, difficult to guess what it does by analogy with other cmdlets. There are only two with a -First parameter (Select-Object and Import-CliXml), but one of them is used very often, so that’s probably what set my expectation.

Suggestion: could it be aliased to -First (and maybe -First $Num) for easier discoverability and easier guessing what it might do?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
KirkMunrocommented, Aug 26, 2019

The docs are poorly written.

List makes sense if it is explained properly. Something like this would be more appropriate:

Use -List to retrieve a list of input files whose contents match the regular expression. The first instance of text that matches the regular expression in each matching input file will be shown after the input file name.

2reactions
BrucePaycommented, Aug 27, 2019

As @KirkMunro points out, the detailed help is not exactly helpful as it appears to completely miss the point of this option. To amplify what he said, the point of -List is to list all of the files that contain the specified pattern. That’s why it’s -List and not -First. It returns the first match because it has to return something and the MatchInfo object has the file name it in it so why not. WRT aliases, they give another name to a parameter they don’t and can’t change it’s type. Adding a new -First N (and -Last N?) parameter would seem to be the way to go…

Read more comments on GitHub >

github_iconTop Results From Across the Web

Select-String (Microsoft.PowerShell.Utility)
Select -String is based on lines of text. By default, Select-String finds the first match in each line and, for each match, it...
Read more >
How to use PowerShell Grep equivalent Select-String
For PowerShell, we can use the grep equivalent Select-String . ... The first digit is the number of lines before, the second is...
Read more >
Select-String - PowerShell Command - PDQ
You can type `Select-String` or its alias, `sls`. Select-String is based on lines of text. By default, Select-String finds the first match in...
Read more >
Select-String - PowerShell
Select -String returns the strings as a unit if it finds the search text in any string. -List Return only the first match...
Read more >
powershell - output filename, not string with select-string
I'm using powershell to "grep" my source code for a particular string. If the string is in the file, I would like the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found