-
Notifications
You must be signed in to change notification settings - Fork 7.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adds an OnlyMatching parameter to Select-String to return only the string that matches the pattern #8965
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A couple of small things I think should probably be fixed, but otherwise solid work from what I can see. 👌
@@ -1048,6 +1048,12 @@ public string[] LiteralPath | |||
[Parameter] | |||
public SwitchParameter List { get; set; } | |||
|
|||
/// <summary> | |||
/// Gets or sets a value indicating if the search contains the specified characters. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a bit unclear, as Select-String already returns data containing the specified characters. Does this parameter affect the search or the result that the user sees or receives?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, the parameter should be affecting the result that the user sees. I will update the comment and re-commit.
src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs
Outdated
Show resolved
Hide resolved
src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs
Outdated
Show resolved
Hide resolved
…chString.cs Co-Authored-By: nanyiyang <nanyiry2@illinois.edu>
…chString.cs Co-Authored-By: nanyiyang <nanyiry2@illinois.edu>
@@ -1048,6 +1048,12 @@ public string[] LiteralPath | |||
[Parameter] | |||
public SwitchParameter List { get; set; } | |||
|
|||
/// <summary> | |||
/// Gets or sets a value that when true, causes only the matching part of the string to be returned. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Worth noting that this causes a string
rather than a MatchInfo
to be returned
How does this change work with I'd probably expect something like this:
|
Is this really a change we want to make? The *nix commands has lots of flags to change the output format of commands, but this is generally not needed in PowerShell since we have structured objects in the pipeline. Tab completion will have to be fixed, since this parameter changes the output type. Can you help me with a scenario where this is change is compelling? |
I want to ping @mklement0 to get his thoughts but @powercode I don't think that accomplishes the same goal.
|
Update-TypeData -Typename Microsoft.PowerShell.Commands.MatchInfo -MemberName MatchingText`
-MemberType ScriptProperty -Value {foreach($m in $this.Matches){foreach($g in $m.Groups){$g.Value}}} Then sls … | foreach MatchingText |
Supporting returning whole lines directly is the subject of #7713 - and both it and the change at hand (matching parts only, via #7712) have been green-lit. The motivation is the same in both cases:
|
@powercode I can get behind that. We can add another property to Select-String "foo" | foreach MatchingText like you said to get this behavior. For context to @nanyiyang... Select-String "foo" returns a collection of Select-String "foo" | foreach MatchingText which says, give me just the |
That is only slightly less verbose than what you have to do currently: # Works with -AllMatches too
PS> 'afoobar' | Select-String 'foo' | foreach { $_.Matches.Value }
foo and wouldn't yield the hoped-for performance improvements - though I suppose it could be implemented as well (though I personally think Also note that |
Specifically, I'm asking for strings to be returned - which is arguably Not having fluff, i.e. not having to access properties in a subsequent step when all you're looking for is strings not only improves performance, but is also concise and convenient. |
I've summed up the rationale for this new switch as well as for |
This PR has been automatically marked as stale because it has not had activity in the last 30 days. It will be closed if no further activity occurs within 10 days. |
This PR has been automatically closed because it is stale. If you wish to continue working on the PR, please first update the PR, then reopen it. |
PR Summary
Part of HackIllinois with @TylerLeonhardt and @rjmholt
Added an OnlyMatching parameter to Select-String with similar behavior to grep -o. Also made OnlyMatching parameter to work with AllMatches.
PR Context
Resolves #7712.
PR Checklist
.h
,.cpp
,.cs
,.ps1
and.psm1
files have the correct copyright headerWIP:
or[ WIP ]
to the beginning of the title (theWIP
bot will keep its status check atPending
while the prefix is present) and remove the prefix when the PR is ready.[feature]
to your commit messages if the change is significant or affects feature tests