Skip to content

Select-String: Add OnlyMatching parameter to return only the actual matching value - #10696

Closed
Chris Gardner (ChrisLGardner) wants to merge 5 commits into
PowerShell:masterfrom
ChrisLGardner:select-string-values-only
Closed

Chris Gardner (ChrisLGardner) wants to merge 5 commits into
PowerShell:masterfrom
ChrisLGardner:select-string-values-only

Conversation

@ChrisLGardner

Copy link
Copy Markdown
Contributor

PR Summary

PR Context

Issue #7712 wanted a -OnlyMatching switch to allow Select-String to only return the text that matched rather than either the full MatchInfo object (default behaviour) or the Line (-Raw behaviour). This is the initial implementation of that.

The issue does suggest allowing this switch to avoid the primary code path that creates the MatchInfo objects in the first place to improve performance since they aren't actually used in the output at all, I'd like to add that functionality but it's a bit beyond my abilities right now but I'll look into it and see if I can add that in either before this PR is merged or in a future PR if the team feels it's a needed improvement.

I had thought to add a parameter set for this new parameter since it doesn't make sense to be able to use both -Raw and -OnlyMatching but that broke parameter binding in a way I didn't expect and wasn't able to see why it would. I can add that back in as part of this PR and try to resolve it should it be required.

PR Checklist

@ChrisLGardner Chris Gardner (ChrisLGardner) changed the title Add OnlyMatching parameter to Select-String to return only the actual matching value Select-String: Add OnlyMatching parameter to return only the actual matching value Oct 3, 2019
@vexx32

Copy link
Copy Markdown
Collaborator

PoshChan-Bot (@PoshChan) please retry Linux

@PoshChan

Copy link
Copy Markdown
Collaborator

Rain Sallow (/u/ta11ow) (@vexx32), successfully started retry of PowerShell-CI-Linux

@vexx32

Copy link
Copy Markdown
Collaborator

I know the -OnlyMatching name was specified by the committee, but it feels very awkward to me in comparison to some cmdlets; Get-ChildItem doesn't have a -OnlyName switch, it's just -Name

Personally I feel like something along the lines of -OnlyMatch would be a little less out of place, or even just -Matches might be OK (though I'm a little wary of that, if only because it aligns too well with the [regex]::Matches() method, which returns full objects).

What do you guys think, Steve Lee (@SteveL-MSFT)? 🙂

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice and straightforward; couple questions on the behaviour. From the linked issue, it sounds like we should be returning the text from all match instances on the line?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public SwitchParameter OnlyMatching { get; set; }
[Alias('o')]
public SwitchParameter OnlyMatching { get; set; }

We may want to add the explicit alias here as mentioned in the related issue.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added the alias.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the alias should be om which is much less ambiguous (-o matches every param that starts with o) and is consistent with -ob for -OutBuffer and -ov for OutVariable.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keith Hill (@rkeithhill) I'd agree, but I think the intent in the original issue was to match grep's existing -o parameter.

@rkeithhill Keith Hill (rkeithhill) Oct 6, 2019 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Considering all the parameters on Select-String that do not match grep parameters, is it worth it to try to force-fit just this one?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that -om is more in keeping with the PowerShell style. Does PowerShellTeam have a preference for what it should be?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I too agree that -om is better; to get a definitive ruling the committee would need to be pinged, but we mere mortals can't do that, I think; Steve Lee (@SteveL-MSFT), any thoughts?

Rain Sallow (/u/ta11ow) (@vexx32), re returning the text from all match instances on the line with -AllMatches: yes, that was the intent; it's how grep -o does it too (albeit invariably); e.g., 'foo' | grep -o 'o'

@mklement0

Michael Klement (mklement0) commented Nov 16, 2019 •

Copy link
Copy Markdown
Contributor

One other thing to implement:

-OnlyMatching should be mutually exclusive with -SimpleMatch, because in the latter case the matching parts of the lines are currently not captured at all, right? At least they don't show up in the output objects.

Note that, similarly, the currently permitted but pointless combination of -SimpleMatch and -AllMatches should be prevented - see #11091

@ghost

Copy link
Copy Markdown

This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days.
Mainainer, Please provide feedback and/or mark it as Waiting on Author

@kilasuit Ryan Yates (kilasuit) added the WG-Cmdlets general cmdlet issues label Jul 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

WG-Cmdlets general cmdlet issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants