0

How to grep for a pattern matching from a text file? I have a string like below.

2018-04-10 08:10:35 M2686678688 abc-jhg-jj-kjk
<accepted> M1236687688</accepted>

I am using like below:

Get-ChildItem $path\a.txt | Select-String "M(\d{10})"

Not sure how to get only the matching string from the above. Desired output should be:

M2686678688 
M123668768
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
San
  • 226
  • 5
  • 14

4 Answers4

2

This should do it, using Select-String to process the file.

It outputs MatchInfo objects for each match, and then you need the first one, and the capture group, and then the value that matched:

$values = select-string -Path "$path\a.txt" -Pattern 'M(\d{10})' |
    ForEach-Object { 
        $_.Matches[0].Groups[1].Value
    }
TessellatingHeckler
  • 27,511
  • 4
  • 48
  • 87
1

All you need to do is remove the capturing group and expand the value of the matches:

Get-ChildItem $path\a.txt |
    Select-String 'M\d{10}' |
    Select-Object -Expand Matches |
    Select-Object -Expand Groups |
    Select-Object -Expand Value

If you have a varying number of digits after the "M" you may also want to adjust your expression to something like M\d+. If the number of digits must be in a specific range you could use a pattern M\d{MIN,MAX}, e.g. M\d{5,} for a string with at least 5 digits, or M\d{7,15} for a string with at least 7 and at most 15 digits.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
1

As you are using "\d{10}" which basically checks 10 time and in your second line sample its only 9 digits hence you are not able to get the second match.

Secondly instead of using get-childItem the same purpose is specified using the path parameter of Get-Content. Get-Content -Path a.txt | %{ [Regex]::Matches($, "M([0-9]*)") } | %{ $.Value } and you will get all the matching results.

0

First, you pattern will never match the second value (M123668768) because you are looking for an M followed by 10 digits whereas the value only has 9. So if you want to match both values the correct regex-pattern would be:

M\d+

Now you can create a Regex object

$regex = [Regex]::new("M\d+")

and get all Matches

$regex.Matches("2018-04-10 08:10:35 M2686678688 abc-jhg-jj-kjk
<accepted> M123668768</accepted>")

To get only the Values of the Matches you can add .Value

$regex.Matches("2018-04-10 08:10:35 M2686678688 abc-jhg-jj-kjk
<accepted> M123668768</accepted>").Value
J. Bergmann
  • 440
  • 3
  • 13