Discussion:
grab a String (server name) from log
(too old to reply)
Dee
2009-06-26 10:33:01 UTC
Permalink
Hi

I'm looking for a way of grabing a string from a log file and creating a
variable from it.

I have looked at the pattern match and Instr functions but non the wiser on
how I can grab a string from a log such as below:

I want to grab the name of the server between the "" in the log file

If anyone has some pointers to the code that I can use that would be great

Log File example:

20080626093137.000000+060,1024, (MS473) Node "myserver.mycomany.com" may be
down. Failed to contact it
--
Dee
Richard Mueller [MVP]
2009-06-26 10:47:54 UTC
Permalink
Post by Dee
I'm looking for a way of grabing a string from a log file and creating a
variable from it.
I have looked at the pattern match and Instr functions but non the wiser on
I want to grab the name of the server between the "" in the log file
If anyone has some pointers to the code that I can use that would be great
20080626093137.000000+060,1024, (MS473) Node "myserver.mycomany.com" may be
down. Failed to contact it
A VBScript solution:
========
Option Explicit
Dim strLogFile, objFSO, objLog, strLine
Dim intIndex1, intIndex2, strServer

Const ForReading = 1

strLogFile = "c:\Scripts\Example.log"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objLog = objFSO.OpenTextFile(strLogFile, ForReading)

Do Until objLog.AtEndOfStream
strLine = objLog.ReadLine
intIndex1 = InStr(strLine, """")
intIndex2 = InStr(intIndex1 + 1, strLine, """")
If (intIndex1 > 0) And (intIndex2 > intIndex1) Then
strServer = Mid(strLine, intIndex1 + 1, intIndex2 - intIndex1 - 1)
Wscript.Echo strServer
End If
Loop
objLog.Close
--
Richard Mueller
MVP Directory Services
Hilltop Lab - http://www.rlmueller.net
--
Dee
2009-06-26 11:54:01 UTC
Permalink
Thanks Richard, I wil give this a go, I managed in the end to use the pattern
below in my script which now matches between the quotes

objRE.Pattern = """.*"""

Cheers
--
Dee
Post by Richard Mueller [MVP]
Post by Dee
I'm looking for a way of grabing a string from a log file and creating a
variable from it.
I have looked at the pattern match and Instr functions but non the wiser on
I want to grab the name of the server between the "" in the log file
If anyone has some pointers to the code that I can use that would be great
20080626093137.000000+060,1024, (MS473) Node "myserver.mycomany.com" may be
down. Failed to contact it
========
Option Explicit
Dim strLogFile, objFSO, objLog, strLine
Dim intIndex1, intIndex2, strServer
Const ForReading = 1
strLogFile = "c:\Scripts\Example.log"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objLog = objFSO.OpenTextFile(strLogFile, ForReading)
Do Until objLog.AtEndOfStream
strLine = objLog.ReadLine
intIndex1 = InStr(strLine, """")
intIndex2 = InStr(intIndex1 + 1, strLine, """")
If (intIndex1 > 0) And (intIndex2 > intIndex1) Then
strServer = Mid(strLine, intIndex1 + 1, intIndex2 - intIndex1 - 1)
Wscript.Echo strServer
End If
Loop
objLog.Close
--
Richard Mueller
MVP Directory Services
Hilltop Lab - http://www.rlmueller.net
--
T Lavedas
2009-06-26 13:37:49 UTC
Permalink
Post by Dee
Thanks Richard, I wil give this a go, I managed in the end to use the pattern
below in  my script which now matches between the quotes
objRE.Pattern = """.*"""
Cheers
--
Dee
Post by Richard Mueller [MVP]
Post by Dee
I'm looking for a way of grabing a string from a log file and creating a
variable from it.
I have looked at the pattern match and Instr functions but non the wiser on
I want to grab the name of the server between the "" in the log file
If anyone has some pointers to the code that I can use  that would be
great
20080626093137.000000+060,1024, (MS473) Node "myserver.mycomany.com" may be
down. Failed to contact it
========
Option Explicit
Dim strLogFile, objFSO, objLog, strLine
Dim intIndex1, intIndex2, strServer
Const ForReading = 1
strLogFile = "c:\Scripts\Example.log"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objLog = objFSO.OpenTextFile(strLogFile, ForReading)
Do Until objLog.AtEndOfStream
    strLine = objLog.ReadLine
    intIndex1 = InStr(strLine, """")
    intIndex2 = InStr(intIndex1 + 1, strLine, """")
    If (intIndex1 > 0) And (intIndex2 > intIndex1) Then
        strServer = Mid(strLine, intIndex1 + 1, intIndex2 - intIndex1 - 1)
        Wscript.Echo strServer
    End If
Loop
objLog.Close
--
Richard Mueller
MVP Directory Services
Hilltop Lab -http://www.rlmueller.net
--
In cases like this, I like to use an approach based on the Split
function. It seems more straightforward to me ...

========
Option Explicit
Dim strLogFile, objFSO, objLog
Dim strServer

Const ForReading = 1

strLogFile = "c:\Scripts\Example.log"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objLog = objFSO.OpenTextFile(strLogFile, ForReading)

Do Until objLog.AtEndOfStream
strServer = split(objLog.ReadLine, Chr(34))(1)
Wscript.Echo strServer
Loop
objLog.Close
========

Tom Lavedas
***********
sw0rdfish
2009-06-26 15:46:18 UTC
Permalink
Post by Dee
Hi
I'm looking for a way of grabing a string from a log file and creating a
variable from it.
--
Dee
Set objFS = CreateObject("Scripting.FileSystemObject")
strFile = "c:\test\file.txt"
Set objFile = objFS.OpenTextFile(strFile)
Do Until objFile.AtEndOfStream
strLine = objFile.ReadLine
s = Split(strLine," ")
For i=LBound(s) To UBound(s)
If InStr(s(i), """" )>0 Then
s(i) = Replace(s(i),Chr(34),"")
WScript.Echo s(i)
End If
Next
Loop


if you can download gawk for windows: http://gnuwin32.sourceforge.net/packages/gawk.htm
here's a one liner

C:\test>gawk "{for(i=1;i<NF;i++){if ($i~/\042/){gsub(/\042/,\"\",
$i);print $i}}}" logfile.txt
myserver.mycomany.com

Loading...