Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista - search log file and write multiple lines

Reply
 
Old 08-23-2009   #1 (permalink)
James


 
 

search log file and write multiple lines

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

My System SpecsSystem Spec
Old 08-23-2009   #2 (permalink)
Paul Randall


 
 

Re: search log file and write multiple lines


"James" <jwanders@xxxxxx> wrote in message
news:mXjkm.146206$Qg6.94354@xxxxxx
Quote:

> 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
If you read the input stream one line at a time, it is easy to save the
three most recent lines and have them available to be written when you find
a line of interest. It is kind of hard to write the three lines following
the line of interest because you have not read them yet.

If the file isn't too big and you have enough memory, you might read the
entire file into a string and then split it into an array of lines, and then
just use a for ... next loop to search for lines of interest and, when
found, and write out the (up to) seven lines, starting with the greater of
(current line minus three, or zero) and ending with the lesser of (current
line plus three, or the line count of the file). A WXP system with 500
MBytes of Ram should be able to handle a 50 MByte text file, I think.

-Paul Randall


My System SpecsSystem Spec
Old 08-23-2009   #3 (permalink)
Richard Mueller [MVP]


 
 

Re: search log file and write multiple lines


"James" <jwanders@xxxxxx> wrote in message
news:mXjkm.146206$Qg6.94354@xxxxxx
Quote:

> 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

--
Richard Mueller
MVP Directory Services
Hilltop Lab - http://www.rlmueller.net
--


My System SpecsSystem Spec
Old 08-23-2009   #4 (permalink)
James


 
 

Re: search log file and write multiple lines

Richard Mueller [MVP] wrote:
Quote:

> "James" <jwanders@xxxxxx> wrote in message
> news:mXjkm.146206$Qg6.94354@xxxxxx
Quote:

>> 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.

Thanks,

James
My System SpecsSystem Spec
Old 08-24-2009   #5 (permalink)
Paul Randall


 
 

Re: search log file and write multiple lines


"James" <jwanders@xxxxxx> wrote in message
news:SWnkm.70160$sC1.36758@xxxxxx
Quote:

> Richard Mueller [MVP] wrote:
Quote:

>> "James" <jwanders@xxxxxx> wrote in message
>> news:mXjkm.146206$Qg6.94354@xxxxxx
Quote:

>>> 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


My System SpecsSystem Spec
Old 08-24-2009   #6 (permalink)
Richard Mueller [MVP]


 
 

Re: search log file and write multiple lines


"James" <jwanders@xxxxxx> wrote in message
news:SWnkm.70160$sC1.36758@xxxxxx
Quote:

> Richard Mueller [MVP] wrote:
Quote:

>> "James" <jwanders@xxxxxx> wrote in message
>> news:mXjkm.146206$Qg6.94354@xxxxxx
Quote:

>>> 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.
>
> 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
--


My System SpecsSystem Spec
Old 08-25-2009   #7 (permalink)
James


 
 

Re: search log file and write multiple lines

Paul Randall wrote:
Quote:

> "James" <jwanders@xxxxxx> wrote in message
> news:SWnkm.70160$sC1.36758@xxxxxx
Quote:

>> Richard Mueller [MVP] wrote:
Quote:

>>> "James" <jwanders@xxxxxx> wrote in message
>>> news:mXjkm.146206$Qg6.94354@xxxxxx
>>>> 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
>
>
I'm not trying to keep people guessing. If the script finds the first
instance of the search string then it writes the previous three lines,
the line with the search string and the subsequent three lines. If any
of the subsequent 3 lines contain the string then the script should
continue to write lines until it reaches the third line from the last
line containing the search string.
My System SpecsSystem Spec
Old 08-25-2009   #8 (permalink)
James


 
 

Re: search log file and write multiple lines

Richard Mueller [MVP] wrote:
Quote:

> "James" <jwanders@xxxxxx> wrote in message
> news:SWnkm.70160$sC1.36758@xxxxxx
Quote:

>> Richard Mueller [MVP] wrote:
Quote:

>>> "James" <jwanders@xxxxxx> wrote in message
>>> news:mXjkm.146206$Qg6.94354@xxxxxx
>>>> 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.
>>
>> 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
>
Thanks. This works well. I'll test it tomorrow with the log files.
My System SpecsSystem Spec
Old 08-25-2009   #9 (permalink)
ekkehard.horner


 
 

Re: search log file and write multiple lines

James schrieb:
Quote:

> 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.
[...]

Using

(a) a ringbuffer to store the lines before the match
(b) a variable to keep track of the lines to be shown after the match
(c) the RegExp efficiently

as in

Class cRB
Private m_aRB, m_nMod, m_nIdx
Public Function init( nElms )
ReDim m_aRB( nElms )
m_nMod = nElms + 1
m_nIdx = -1
Set init = Me
End Function
Public Sub store( sLine )
m_nIdx = (m_nIdx + 1) Mod m_nMod
m_aRB( m_nIdx ) = sLine
End Sub
Public Sub dump()
Dim nIdx : nIdx = (m_nIdx + 1) Mod m_nMod
Dim nCnt
For nCnt = 1 To m_nMod
If Not IsEmpty( m_aRB( nIdx ) ) Then WScript.Echo m_aRB( nIdx )
nIdx = (nIdx + 1) Mod m_nMod
Next
End Sub
End Class

Dim oFS : Set oFS = CreateObject( "Scripting.FileSystemObject" )
Dim sFSpec : sFSpec = oFS.GetAbsolutePathName( ".\linesincontext.txt" )
Dim nLB : nLB = 1
Dim nLA : nLA = 1

Dim sPat
For Each sPat In Array( "Line", "Nix", "0", "1", "2", "3", "7", "8", "X", "\d" )
WScript.Echo "------- Looking for", sPat
Dim oRE : Set oRE = New RegExp
oRE.Pattern = sPat
Dim oRB : Set oRB = New cRB.init( nLB )
Dim tsIn : Set tsIn = oFS.OpenTextFile( sFSpec )
Dim nLast : nLast = -1
Do Until tsIn.AtEndOfStream
Dim nLine : nLine = tsIn.Line
Dim sLine : sLine = tsIn.ReadLine()
oRB.store sLine
If nLine < nLast Then
WScript.Echo sLine
End If
If oRE.Test( sLine ) Then
If -1 = nLast Or nLine > nLast Then
oRB.dump
End If
nLast = nLine + nLA + 1
End If
Loop
tsIn.Close
Next

output:

=== LinesInContext02: show found lines in context (ringbuffer) (02) ===========
------- Looking for Line
Line 0
Line 1
Line 2 X
Line 3 X
Line 4
Line 5
Line 6
Line 7 X
Line 8
------- Looking for Nix
------- Looking for 0
Line 0
Line 1
------- Looking for 1
Line 0
Line 1
Line 2 X
------- Looking for 2
Line 1
Line 2 X
Line 3 X
------- Looking for 3
Line 2 X
Line 3 X
Line 4
------- Looking for 7
Line 6
Line 7 X
Line 8
------- Looking for 8
Line 7 X
Line 8
------- Looking for X
Line 1
Line 2 X
Line 3 X
Line 4
Line 6
Line 7 X
Line 8
------- Looking for \d
Line 0
Line 1
Line 2 X
Line 3 X
Line 4
Line 5
Line 6
Line 7 X
Line 8
=== LinesInContext02: 0 done (00:00:01) =======================================

should solve your problem in a general way.
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
how to get powershell to write to event log or write a log file? PowerShell
append multiple lines to otherPager field in AD PowerShell
multiple search and replaces in a text file VB Script
Start-Demo / foreach multiple lines PowerShell
SQL store procedure with multiple lines PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46