Forum Discussion
lionheart1981
Oct 05, 2019Copper Contributor
Assistant creating array?
Hi, I'm a Powershell Amateur! I've written some code to import data from logfile.txt and export the results with export-csv to a weapons_date. I'm replicating each line but I'm sure I might be ab...
BrettMiller
Oct 14, 2019Copper Contributor
lionheart1981
Looking at this it could be much simpler but without seeing the content of the logs it's difficult to say for sure.
It looks like you just want the count of each of the matched patterns and add the relevant property as per the match. You can grossly simplify this using Group-Object
Get-Content .\testfile.txt | Where-Object { $_ -match 'weapon_(sks|rpk)' } |
Group-Object |
Select-Object Count, Name, @{name='Weapon';expression={$_.name -replace '.+_'}}
This will give the following output
Count Name Weapon
----- ---- ------
18 weapon_rpk rpk
11 weapon_sks sks