none
Struggling substringing within an Array Pattern RRS feed

  • General discussion

  • Hi, I have some clunky code and I'm sure it can be better. Looking for suggestions. I search a file and to file lines before a search pattern. Sample log file.

    Date Line 1 - Blah Blah Blah Dynamic Text

    Date Line 2 - foo bar 2

    Date Line 3 - foo bar 3

    Date Line 4 - foo bar 4

    In the example below, $searchPattern = "foo bar 4". What I want to find is all occurrences of "Blah Blah Blah" and capture the Dynamic Text and only get a result set that is distinct (I don't want the same Dynamic Text duplicated). the Code below sort of works, I'm stuck trying to substring out just want I need. Thought I could do something like this: select PATINDEX('Blah\sBlah\sBlah.*', $newStringArray), but the interpreter does not like it, even tried moving it to a string.

    Any suggestions? 

    Thank you!

    foreach ($element in $drives) {
        write-host "Searching $element\$FileName" 
         $foundArray = get-childitem $element\$FileName | select-string  -Context 6,0 -pattern $searchPattern 
    
        if ($foundArray[0] -ne " ") {
           $newArray =@()
           $newstringArray =@()
           write-host $foundArray.Count
           #Split up the log lines via carriege returns
           $newArray = $foundArray -split  '[\r\n]' |? {$_}  
            $newStringArray = $newArray | select-string "Blah Blah Blah"
          write-host $newstringArray[0] 
              
         
          break 
        }     
       
    }


    Chris

    Friday, October 3, 2014 12:45 PM

All replies

  • Does this work for your data?

    $regex = '.+blah blah blah(.+)'
    $ht = @{}
    
    (Get-Content samplelog.txt) -match $regex -replace $regex,'$1' |
      foreach { $ht[$_] = $true }
    
    $newstringArray = $ht.keys


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    Friday, October 3, 2014 1:26 PM
    Moderator
  • Thanks. I'm much closer. I couldn't take you suggestion directly to the file because I have to only search for searchpattern2 "if" it is combined with searchpattern .Took your ideas as added the below.. but the replace is not working. If the text I'm searching is "Date Line 1 - Blah Blah Blah Dynamic Text", and I want Dynamic Text. $searchPattern2 = "+Blah Blah Blah(.+)" should work?

           $newArray -match $searchPattern2 -replace $searchPattern2,'$1' | foreach { $ht[$_] = $true }

    Thank you,

    Chris


    Chris

    Friday, October 3, 2014 2:21 PM
  • Thanks. I'm much closer. I couldn't take you suggestion directly to the file because I have to only search for searchpattern2 "if" it is combined with searchpattern .Took your ideas as added the below.. but the replace is not working. If the text I'm searching is "Date Line 1 - Blah Blah Blah Dynamic Text", and I want Dynamic Text. $searchPattern2 = "+Blah Blah Blah(.+)" should work?

           $newArray -match $searchPattern2 -replace $searchPattern2,'$1' | foreach { $ht[$_] = $true }

    Thank you,

    Chris


    Chris

    Can you elaborate on the multiple search patterns a little more?


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "


    Friday, October 3, 2014 3:54 PM
    Moderator
  • Sure, let me modify my example. Given the below, I search through a log file that contains "Date Line 4 - foo bar 4 Search Text" and also pull the first 6 log entries prior to "Date Line 4 - foo bar 4 Search Text". In the 6 lines is where I need to search for "Blah Blah Blah Dynamic Text".  I cannot just search for "Blah Blah Blah Dynamic Text", because must be part of the log statements containing "Date Line 4 - foo bar 4 Search Text"  These are debug logs, hence, I'm finding an error on one log line, then getting the detail on other log-lines in the same series of debug statements. 

    Date Line 1 - Blah Blah Blah Dynamic Text

    Date Line 2 - foo bar 2

    Date Line 3 - foo bar 3

    Date Line 4 - foo bar 4 Search Text

    Thank you,

    Chris


    Chris

    Friday, October 3, 2014 4:48 PM
  • Extrapolating from your test data:

    @'
    Date Line 1 -  Blah Blah Blah Dynamic Text1
    Date Line 2 - foo bar 2
    Date Line 3 - foo bar 3
    Date Line 4 - foo bar 4 Search Text
    Date Line 1 - Blah Blah Blah Dynamic Text2
    Date Line 2 - foo bar 2
    Date Line 3 - foo bar 3
    Date Line 4 - foo bar 4 Search Text
    Date Line 1 - Blah Blah Blah Dynamic Text1
    Date Line 2 - foo bar 2
    Date Line 3 - foo bar 3
    Date Line 4 - foo bar 4 Search Text
    '@ | set-content testlog.log
    
    [regex]$regex = @'
    (?ms).+?Blah Blah Blah(.+?)
    .+?foo bar 2
    .+?foo bar 3
    .+?foo bar 4 Search Text
    '@
    
    $log = Get-Content testlog.log -Raw
    
    $regex.Matches($log) |
     foreach {$_.groups[1].value} |
     sort -Unique
    
    
     Dynamic Text1
     Dynamic Text2


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    Friday, October 3, 2014 8:49 PM
    Moderator