Re: search log file and write multiple lines
- From: "Richard Mueller [MVP]" <rlmueller-nospam@xxxxxxxxxxxxxxxxxxxx>
- Date: Mon, 24 Aug 2009 08:52:24 -0500
"James" <jwanders@xxxxxxxxx> wrote in message
news:SWnkm.70160$sC1.36758@xxxxxxxxxxxxxxx
Richard Mueller [MVP] wrote:
"James" <jwanders@xxxxxxxxx> wrote in messageThanks. When I ran this script it wrote the 3 lines after the line
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
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.
Thanks,
James
Sorry, I didn't test the script. I reversed the order of the code to retain
the 3 previous lines. These lines:
' Retain previous 3 lines.
strPrev1 = strLine
strPrev2 = strPrev1
strPrev3 = strPrev2
should instead be:
' Retain previous 3 lines.
strPrev3 = strPrev2
strPrev2 = strPrev1
strPrev1 = strLine
--
Richard Mueller
MVP Directory Services
Hilltop Lab - http://www.rlmueller.net
--
.
- Follow-Ups:
- Re: search log file and write multiple lines
- From: James
- Re: search log file and write multiple lines
- References:
- search log file and write multiple lines
- From: James
- Re: search log file and write multiple lines
- From: Richard Mueller [MVP]
- Re: search log file and write multiple lines
- From: James
- search log file and write multiple lines
- Prev by Date: Re: ADODB.Connection Problem. Need help troubleshooting.
- Next by Date: Re: WshShell.Exec workaround
- Previous by thread: Re: search log file and write multiple lines
- Next by thread: Re: search log file and write multiple lines
- Index(es):
Relevant Pages
|