Re: search log file and write multiple lines

Tech-Archive recommends: Fix windows errors by optimizing your registry




"James" <jwanders@xxxxxxxxx> wrote in message
news:SWnkm.70160$sC1.36758@xxxxxxxxxxxxxxx
Richard Mueller [MVP] wrote:
"James" <jwanders@xxxxxxxxx> wrote in message
news:mXjkm.146206$Qg6.94354@xxxxxxxxxxxxxxx
Hi,

Ive written some code to search a log file for a specified pattern and
write the lines containing the pattern to a separate text file. In
addition, I want the script to write the 3 lines above the line
containing the pattern and the 3 lines after the line with the pattern.
Is this possible? Here is some of the code. So far I can get it to
write each line containing the pattern.

'Open the file for reading search text
'--------------------------------------

Dim sFAll : sFAll = oFS.OpenTextFile( sSrcFSpec).ReadAll()


'Construct a copy of the destination files
'----------------------------------------

sDstFSpec = strFolder & "/" & "logextract.txt"
Set tsDst = oFS.CreateTextFile( sDstFSpec, True )
strAnswer = InputBox("Please enter the text you want to search for:", _
"Create File")

If strAnswer = "" Then
Wscript.Quit
Else
Wscript.Echo strAnswer
End If

oRE.Pattern = strAnswer
oRE.Global = True


Dim oMT
Do Until tsSrc.AtEndOfStream
Dim sLine : sLine = Trim( tsSrc.ReadLine() )
set colMatches = oRE.Execute(sLine)
if "" <> sLine and colMatches.count=1 then
tsDst.WriteLine sLine
next
end if

In your example, oRE is not Set, so we don't know what it is. I would use
the FileSystemObject to read one line at a time and always retain the 3
previous lines. Then when the given string is found, set a flag so we can
also output the following 3 lines. The following assumes the string is
never found in lines less than 3 lines apart:
===========
Const ForReading = 1
Const ForWriting = 2
Const OpenAsASCII = 0
Const CreateIfNotExist = True

' Specify the files.
strInput = "C:\scripts\example.log"
strOutput = "C:\scripts\logextract.txt"

' Specify string to search for in the file.
strSearch = InputBox("Enter text to search for")

' Open the files.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objInput = objFSO.OpenTextFile(strInput, ForReading)
Set objOutput = objFSO.OpenTextFile(strOutput, _
ForWriting, CreateIfNotExist, OpenAsASCII)

' Read the input file one line at a time.
strPrev3 = ""
strPrev2 = ""
strPrev1 = ""
blnFound = False
strNext1 = ""
strNext2 = ""
strNext3 = ""
Do Until objInput.AtEndOfStream
strLine = objInput.ReadLine
' If the string was previously found, output the next 3 lines.
If (blnFound = True) Then
objOutput.WriteLine strLine
intCount = intCount - 1
' After 3 lines, turn off the flag.
If (intCount = 0) Then
blnFound = False
End If
End If
' Search for text in this line.
' The LCase function makes the search case insensitive.
If (InStr(LCase(strLine), LCase(strSearch)) > 0) Then
' Output the 3 previous lines.
objOutput.WriteLine strPrev3
objOutput.WriteLine strPrev2
objOutput.WriteLine strPrev1
' Output the line with the specified text.
objOutput.WriteLine strLine
' Flag to output the next 3 lines.
blnFound = True
intCount = 3
End If
' Retain previous 3 lines.
strPrev1 = strLine
strPrev2 = strPrev1
strPrev3 = strPrev2
Loop

' Clean up.
objOutput.Close
objInput.Close

Thanks. When I ran this script it wrote the 3 lines after the line
containing the search string but it wrote the previous line three times
like this:

ab
ab
ab
bc
cd
de
ef

Also I think the log files will contain the search string on lines less
than three lines apart.


Why exactly do you make us play guessing games about what you want to happen
when the search string occurs on lines less than three lines apart? For
example, if every line contains the search string, do you want seven lines
of output for every line of the file?

-Paul Randall


.



Relevant Pages

  • Re: search log file and write multiple lines
    ... Ive written some code to search a log file for a specified pattern and write the lines containing the pattern to a separate text file. ... ' Specify string to search for in the file. ... objOutput.WriteLine strPrev1 ... Also I think the log files will contain the search string on lines less than three lines apart. ...
    (microsoft.public.scripting.vbscript)
  • Re: search log file and write multiple lines
    ... write the lines containing the pattern to a separate text file. ... ' Specify string to search for in the file. ... objOutput.WriteLine strLine ... objOutput.WriteLine strPrev1 ...
    (microsoft.public.scripting.vbscript)
  • Re: search log file and write multiple lines
    ... Ive written some code to search a log file for a specified pattern and write the lines containing the pattern to a separate text file. ... ' Specify string to search for in the file. ... objOutput.WriteLine strLine ... objOutput.WriteLine strPrev1 ...
    (microsoft.public.scripting.vbscript)
  • Re: search log file and write multiple lines
    ... Ive written some code to search a log file for a specified pattern and write the lines containing the pattern to a separate text file. ... Then when the given string is found, set a flag so we can also output the following 3 lines. ... ' Specify string to search for in the file. ... objOutput.WriteLine strPrev1 ...
    (microsoft.public.scripting.vbscript)
  • Re: search log file and write multiple lines
    ... write the lines containing the pattern to a separate text file. ... ' Specify string to search for in the file. ... intCount = intCount - 1 ... objOutput.WriteLine strPrev1 ...
    (microsoft.public.scripting.vbscript)