Reputation: 31548
I have a text file that contains the following data:
1390 resource core\scripts\script.properties
1395 resource core\scripts\script.xml
1396 resource core\scripts\script_customer.xml
...
and so on.
I also have a list of args, e.g. file1.txt
, file2.txt
, etc.
What I'd like to do is to extract the numeric id, if exists, by given file name.
I wrote the following to read the contents of a file into a collection:
$regex = "^(?<code>\d+)\s+(?<descriptor>\w+)\s+(?<filename>.*)$"
$results = Get-Content .\resources.txt |
ForEach-Object {
if ($_ -match $regex) {$matches} else { return }
} |
select –Property @{name='code'; expression={$_.code} },
@{name='descriptor'; expression={$_.descriptor} },
@{name='filename'; expression={$_.filename} }
Which gives me all the entries from the file in a nice list.
How can I now query the $results
, to give me the code
for any entry that contains the filename in args?
e.g. in C# LINQ, I'd do something like:
from arg in args
from result in results
where result.fileName.Contains(arg)
select result.code
Upvotes: 0
Views: 674
Reputation: 1151
Try this (PSv3+):
$results | where-object filename -match $arg | select-object -expand code
Upvotes: 1