Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to filter a select-string to only display the found word with wildcard

I am using

Select-String -Path "some\path\smthing.xml" -pattern "smthing*"

which displays the whole line that contains this pattern.
The problem is that there are random numbers after smthing and are always different so I can only use wildcard.

How to filter that result to display only the matching string, ie – smthing44326 instead of the whole line of the file?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

First you need to use a pattern to match specifically smthing followed by one or more numbers (\d+), then once you have the right pattern you need to reference the .Matches.Value property from the objects outputted by Select-String:

Select-String -Path some\path\smthing.xml -Pattern 'smething\d+' | 
    ForEach-Object { $_.Matches.Value }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading