Skip to content

Add a switch to Select-String that returns the matching parts only, analogous to grep -o #7712

Open

Description

Related: #7713 and #7867

Sometimes all you want Select-String to do is to output only the matching parts of the input lines as strings, similar to what grep -o does on Unix-like platforms; e.g.:

# Extract only the parts that match the regex, each on its own line.
PSonUnix> "line1`nline2`nline3" | grep -o '[0-9]'
1
2
3

The equivalent Select-String solution is currently cumbersome:

PS> "line1", "line2", "line3" | Select-String '[0-9]' | ForEach-Object { $_.Matches[0].Value }
1
2
3

If we introduced a switch named, say, -MatchingPartOnly (name TBD, could have an alias of -o)
-OnlyMatching (see decision below), the command could be simplified to:

PS> "line1", "line2", "line3" | Select-String '[0-9]' -OnlyMatching

As an alias, -om could be considered (just -o could break existing code that used it for -OutVariable).

This would also speed up processing, because constructing [Microsoft.PowerShell.Commands.MatchInfo] instances can be bypassed.

Environment data

Written as of:

PowerShell Core 6.1.0-preview.4

Activity

  1. added
    Issue-Enhancement
    on Sep 6, 2018
  2. iSazonov commented on Sep 6, 2018

    @iSazonov
    Collaborator

    Steve Lee (@SteveL-MSFT) Can you approve?

  3. iSazonov commented on Sep 6, 2018

    @iSazonov
    Collaborator

    I think this should works for -SimpleMatch too.

  4. added
    Review - Committee
    on Sep 10, 2018
  5. SteveL-MSFT commented on Nov 7, 2018

    @SteveL-MSFT
    Member

    @PowerShell/powershell-committee reviewed this, seems fine to add but should match the grep description of the parameter and call it -OnlyMatching

  6. added
    Committee-Reviewed
    Up-for-Grabs
    and removed
    Review - Committee
    on Nov 7, 2018
  7. iSazonov commented on Nov 8, 2018

    @iSazonov
    Collaborator

    The OnlyMatching name doesn't correlate with "output" 😕

  8. added
    First-Time-Issue
    on Jan 1, 2019
  9. added
    Hacktoberfest
    on Feb 19, 2019
  10. HumanEquivalentUnit commented on Mar 27, 2019

    @HumanEquivalentUnit
    Contributor

    Sometimes all you want Select-String to do is to output only the matching parts of the input lines as strings

    Seems like a lot of the time, that's all I want -match to do as well; and this would get roughly as close as my imaginary -keep operator (the inverse of -replace):

    PS C:\> 'a word and here' | Select-String -AllMatches -MatchingPartOnly -Pattern'\w{4}'
    word
    here
    PS C:\> 'a word and here' | sls -a -m '\w{4}'
    word
    here
    PS C:\> 'a word and here' -keep '\w{4}'
    word
    here

    😃 Yay!

  11. mklement0 commented on Mar 27, 2019

    @mklement0
    ContributorAuthor

    HumanEquivalentUnit, I quite like the idea of a -keep (or perhaps -extract) operator . Can I suggest you create a feature request for it?

  12. HumanEquivalentUnit commented on Mar 27, 2019

    @HumanEquivalentUnit
    Contributor

    Michael Klement (@mklement0) I did; #7958 but it looked like a duplicate at the time, of your linked issue which is basically the same request.

  13. 5 remaining items

  14. iSazonov commented on May 28, 2020

    @iSazonov
    Collaborator

    I believe we could have an symmetry and PowerShell Committee could discuss both cases:

    • -SimpleMatch + -AllMatches
    • -OnlyMatching + -AllMatches
  15. vazome commented on Aug 8, 2020

    @vazome

    If someone curious about a current way of doing so, you can use this:

    $value = 'aa' #or even [System.IO.File]::ReadLines($filename)
    $options = [Text.RegularExpressions.RegexOptions]::IgnoreCase -bor [Text.RegularExpressions.RegexOptions]::CultureInvariant
    $regex = 'REGEX'
    $properselectstring = [regex]::Matches($value,$regex,$options)
    $properselectstring.value
    
  16. Kissaki commented on Oct 16, 2021

    @Kissaki

    This issue is labeled Up-for-Grabs but PR #10696 seems to tackle it? If so, wouldn't it be adequate to remove the up for grabs label?

  17. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  18. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  19. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  20. added
    Resolution-No Activity
    on Nov 16, 2023
  21. microsoft-github-policy-service commented on Nov 23, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.

  22. added
    KeepOpen
    and removed
    Resolution-No Activity
    on Oct 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Committee-ReviewedFirst-Time-IssueHacktoberfestIssue-EnhancementKeepOpenUp-for-GrabsWG-Cmdlets

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions