Announcement

Collapse
No announcement yet.

Looking for a script to parse/extract text from log files

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Looking for a script to parse/extract text from log files

    Being something of a novice, I know enough to draw a single line of text from an ascii file but I would like to capture selected text from multiple lines if possible. Well, needless to say, I'm having a time doing it and here is my situation.

    I would like to search through a folder of log files (usually 1000 or more) and find any line within a log file that has the text 'ERROR:' (see below). I would then capture most of that line and then a portion of the text in the line just above it, outputting this yet into a second text file.

    ExampleLogFile.txt
    .................................................. .............
    10/10/2010 10:10:01 AM Uploading Document: ABCD_123456789P001234567
    10/10/2010 10:11:18 AM ERROR: The underlying connection was closed: The connection was closed unexpectedly.
    .................................................. .............


    Text to be captured from second line (Date, Time, Complete error message)

    Text to be captured from line above (last 24 characters): ABCD_123456789P001234567


    ExampleOutputFile.txt
    .................................................. .............
    10/10/2010 <tab> 10:11:18 AM <tab> ABCD_123456789P001234567 <tab> The underlying connection was closed: The connection was closed unexpectedly.
    .................................................. .............

    Is this even feasible? Any ideas are very welcome!

  • #2
    Re: Looking for a script to parse/extract text from log files

    Here's one way to do it with VBScript. Save this snippet as Something.vbs and call it like so:

    Code:
    cscript /nologo Something.vbs ExampleLogFile.txt
    Code:
    Dim fso
    Dim c	' File contents (Array)
    Dim i	' Loop counter
    Dim p	' Position of search string
    Dim s	' Search String
    
    s = "ERROR:"
    
    Set fso = CreateObject("Scripting.FileSystemObject")
    
    c = Split(fso.OpenTextFile(WScript.Arguments(0), 1, False).ReadAll, vbCrLf)
    
    For i = 0 To UBound(c)
    	p = InStr(1, c(i), s, vbTextCompare)
    	
    	If p > 0 Then
    		WScript.StdOut.WriteLine Left(c(i), p-1) & vbTab & Right(c(i-1), 24) & vbTab & Mid(c(i), p+(Len(s)+1))
    	End If
    Next
    
    Set fso = Nothing
    This was a quick one, so please add error checking. Good luck!
    Jay Adams, Noxigen LLC

    Comment


    • #3
      Re: Looking for a script to parse/extract text from log files

      You absolutely ROCK! This was exactly what I was looking for to get me further down the path. And given your approach, I really am a novice bordering on n00b!

      Thanks for the script!

      Comment

      Working...
      X