Applying a regex group capture in overall select-object expression

Welcome Forums General PowerShell Q&A Applying a regex group capture in overall select-object expression

This topic contains 2 replies, has 3 voices, and was last updated by

1 year, 4 months ago.

  • Author
  • #82037

    Points: 0
    Rank: Member

    I am trying to parse some xml log entries and export the data as rows in a CSV file. The format of the input file looks like:


    The Messages envelope contains a couple of thousands of Message children.

    I am trying to extract the values of two attributes in their raw form and for the third attribute I want to apply a regular expression that captures a group and retain the group match. E.g. The custom object I would like to end up in the end should have attributes "value1", "value2", and Regex group match of "value3".

    What I have at the moment is

    [xml]$results = get-content log_messages.log
    $results.LogMessages.LogMessage | Where-Object {$_.messageID -like "S6F*"} |
        Select-Object -Property time,message,'#cdata-section' |
        Export-Csv parse_results.csv -Delimiter "," -NoTypeInformation

    This yields a csv file with three columns, one for each property. What I really need is that the third column be the capture group applied on the value in '#cdata-section'. How do I apply the capture group in the overall query?

    I am a complete beginner and I thank you in advance for your time.

  • #82127

    Points: 1,811
    Helping HandTeam Member
    Rank: Community Hero

    XML can't be pasted here; you'll need to put it into a Gist and include the Gist URL here.

  • #82133

    Points: 1
    Rank: Member

    Well, without being able to see the sample data this can't be exact, but in general, you can use a calculated property with Select-Object to do your RegEx on '#cdata-section'. It could look something like this.

    [xml]$results = get-content log_messages.log
    $results.LogMessages.LogMessage |
    Where-Object {$_.messageID -like "S6F*"} |
    Select-Object -Property time,message,@{Label = 'Value3'; Expression = {[regex]::Match($_.'#cdata-section','pat(t.*)ern').Groups[1].value}} |
    Export-Csv parse_results.csv -Delimiter "," -NoTypeInformation

The topic ‘Applying a regex group capture in overall select-object expression’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort