How do I return only the matching regular expression when I select-string(grep) in PowerShell?

85,499

Solution 1

David's on the right path. [regex] is a type accelerator for System.Text.RegularExpressions.Regex

[regex]$regex = '.-.-.'
$regex.Matches('abc 1-2-3 abc') | foreach-object {$_.Value}
$regex.Matches('abc 1-2-3 abc 4-5-6') | foreach-object {$_.Value}

You could wrap that in a function if that is too verbose.

Solution 2

Or just:

Select-String .-.-. .\test.txt -All | Select Matches

Solution 3

I tried other approach: Select-String returns property Matches that can be used. To get all the matches, you have to specify -AllMatches. Otherwise it returns only the first one.

My test file content:

test test1 alk atest2 asdflkj alj test3 test
test test3 test4
test2

The script:

select-string -Path c:\temp\select-string1.txt -Pattern 'test\d' -AllMatches | % { $_.Matches } | % { $_.Value }

returns

test1 #from line 1
test2 #from line 1
test3 #from line 1
test3 #from line 2
test4 #from line 2
test2 #from line 3

Select-String at technet.microsoft.com

Solution 4

In the spirit of teach a man to fish ...

What you want to do is pipe the output of your select-string command into Get-member, so you can see what properties the objects have. Once you do that, you'll see "Matches" and you can select just that by piping your output to | **Select-Object** Matches.

My suggestion is to use something like: select linenumber, filename, matches

For example: on stej's sample:

sls .\test.txt -patt 'test\d' -All |select lineNumber,fileName,matches |ft -auto

LineNumber Filename Matches
---------- -------- -------
         1 test.txt {test1, test2, test3}
         2 test.txt {test3, test4}
         3 test.txt {test2}

Solution 5

None of the above answers worked for me. The below did.

Get-Content -Path $pathToFile | Select-String -Pattern "'test\d'" | foreach {$_.Matches.Value}

Get-Content -Path $pathToFile | # Get-Content will divide into single lines for us

Select-String -Pattern "'test\d'" | # Define the Regex

foreach {$_.Matches.Value} # only return the value of the Object's Matches field. (This allows for multiple result matches.)

Share:
85,499
Skyler
Author by

Skyler

Updated on October 28, 2021

Comments

  • Skyler
    Skyler over 2 years

    I am trying to find a pattern in files. When I get a match using Select-String I do not want the entire line, I just want the part that matched.

    Is there a parameter I can use to do this?

    For example:

    If I did

    select-string .-.-.
    

    and the file contained a line with:

    abc 1-2-3 abc
    

    I'd like to get a result of just 1-2-3 instead of the entire line getting returned.

    I would like to know the Powershell equivalent of a grep -o