Select-String & RegEx

Welcome Forums General PowerShell Q&A Select-String & RegEx

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

2 years, 7 months ago.

  • Author
  • #54720

    Topics: 1
    Replies: 1
    Points: 21
    Rank: Member

    I'm having a problem with RegEx. Every time I think I have it down the parameters of what i need to do change and I'm reminded how much RegEx can hurt your brain!

    What I am currently trying to do is extract a certain string of an unknown length from a big text file. It's an event security log from a 3rd party to be exact. for most (if not all) I know the text before and after my string. Here is an example of what I need to look for
    Subject.Account_Name: SourceUserName Subject.Account_Domain: CORP

    From that string I need to extract SourceUserName That will change for each line.

    I have multiple logs that i need to parse through and for the most part know the beginning and ending string.
    Can anybody help with the RegEx search OR if there is a suggestion on a better way to do this i am open.

  • #54723

    Topics: 1
    Replies: 1325
    Points: 1,651
    Helping Hand
    Rank: Community Hero

    The regex is the easiest part I think:

    'Subject.Account_Name: SourceUserName Subject.Account_Domain: CORP' -match 'Subject\.Account_Name:\s+(.+)\s+Subject\.Account_Domain:\s+CORP'

    For the rest you should do a foreach loop and do whatever you need to do with the results.
    If you have some trouble with your code you can come back and we're gona help you. 😉

  • #54724

    Topics: 3
    Replies: 7
    Points: 0
    Rank: Member

    Something akin to:

     ls c:\temp\searchfolder -recurse | Select-String "google" | Select Path, LineNumber | Format-List OR $Regex = "\b\D*([\d]{1,}).*\b"

    Perhaps this may help? RegEx Searches I'm no expert but I seen the question figured I'd try run through some oddball searches of google in hopes of helping.

  • #54727

    Topics: 3
    Replies: 262
    Points: 68
    Helping Hand
    Rank: Member

    I recommend using the switch statement as it can be more efficient compared to Get-Content depending on the size and number of log files.

    $logs = Get-ChildItem \path\to\logfiles\logs*
    $strings = foreach ($log in $logs){
    switch -Regex -File $log{
    'Account_Name: (.*) Subject' {$Matches[1]}

The topic ‘Select-String & RegEx’ is closed to new replies.

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