Reputation: 13
I have a text file and I am using Powershell to list out the names present in the below pattern
Contents of the file:
beta-clickstream-class="owner:"mike""
beta-clickstream-class="owner:"kelly""
beta-clickstream-class="owner:"sam""
beta-clickstream-class="owner:"joe""
beta-clickstream-class="owner:"john""
beta-clickstream-class="owner:"tam""
Output I am looking for
mike
kelly
sam
joe
john
tam
Script I am using is
$importPath = "test.txt"
$pattern = 'beta-clickstream-class="owner:"(.*?)""'
$string = Get-Content $importPath
$result = [regex]::match($string, $pattern).Groups[1].Value
$result
Above script is only listing the first name on the file. Can you please guide me on how to list all the names on the file.
Upvotes: 1
Views: 714
Reputation: 437743
Get-Content
returns an array of strings, so you would have to call [regex]::match()
on each element of array $string
.
However, the -replace
operator, as suggested by AdminOfThings, enables a simpler solution:
(Get-Content $importPath) -replace '.+owner:"([^&]+).+', '$1'
Alternatively, you could have read the file into a single, multi-line string with Get-Content -Raw
, followed by [regex]::Matches()
(multiple matches), not [regex]::Match()
(single match).
Upvotes: 2