regexpowershellsearchdemo

Searching for a matching map name in Powershell


sorry i really dont know how to properly ask this question. I would like to parse CS:GO Demo files in Powershell, and i would like to retrive the map name from it.

I opening dem files like this: Get-Content $demo | Select -First 1 | Select-String -Pattern 'de_'

And i get this as response:


HL2DEMO    đ5  MatchServer I.                                                                                                                                                                
                                                                        GOTV Demo                                                                                                                           
                                                                                                                                de_mirage                                                                   
                                                                                                                                                                                        csgo                
                                                                                                                                                                                                            
                                     @#A  g   uÔ ~ř˙˙                                                                                                                                                   
            ą     Vđk (8wEÄü€ŢMĐhZăU    X@`śh u   <zcsgo‚   de_mirageŠ ’sky_dustšGOTV¨ ° ¸      ( 0 ž


I would like to get only the de_mirage as a variable. So if a map changes, then it will be de_dust2 or de_inferno and so on. Does anybody know a solution for this?

Thank you!


Solution

  • When using Get-Content, each line is passed down the pipeline one at a time, unless specifying the -Raw switch. The reason I bring this up is due to your Select cmdlet that you're piping to. When you specified the parameter of -First, with a value of 1, you're only grabbing the first line, and then trying to find the pattern in the first line.

    Here's my poor attempt at RegEx:

    Get-Content -Path $demo | Where-Object -FilterScript { $_ -match 'de_\w+' }
    $Matches[0]
    

    . . .where the $Matches Automatic Variable contains all the matched RegEx patterns (as the name indicates) stored in an array format; where we use the index number to reference the value. This would also work piping to Select-String when searching for a Pattern just like you had done.