Emile
Emile

Reputation: 602

Powershell - How to find a pattern in a file

I have in a file the output of a Visual Studio project build. I want to read that file, find the pattern and remove everthing in the file except that pattern itself.

The pattern to find looks like 23 failed

The text file looks like that :

Microsoft Visual Studio  
Copyright (C) Microsoft Corp. All rights reserved.  
1>------ Build started: Project: Test, Configuration: Debug ------  
1>   Creating library S:\Test\Test.lib and object S:\Test\Test.exp  
1>Test.vcxproj -> S:\Test\Debug\Test.dll  
========== Build: 1 succeeded, 23 failed, 12 up-to-date, 0 skipped ==========

I've tried this, but it didn't work out:

$var = (Get-Content "$Path") -replace 's/.*(\d)+ failed.*'

Upvotes: 0

Views: 102

Answers (1)

PlageMan
PlageMan

Reputation: 792

Instead of doing a replace you can use name group to extract what you want :

Get-Content $Path | % { if ($_ -match '(?<FailCount>\d+ failed)') { $Matches["FailCount"] } }

Upvotes: 1

Related Questions