Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

extract text between quotes in a log file 1

Status
Not open for further replies.
Oct 9, 2003
174
0
0
US
I am looking to write a script that will read in a log file line by line, extract the values between the two sets of quotes, output those values to another file then exit. My log file is a number of lines like the following:

152.15.112.88:1755 ssl "(CN=Hung\,Bart,CN=Users,DC=company,DC=com)@(LDAP)" "15/Apr/2008:10:26:38.779 -0400" v5 connect 13552

I know how to read the log file in line by line and I know how to output the values once I found them. The question I have is how can write something to extract the characters between the two sets of quotes.

Like this:

(CN=Hung\,Bart,CN=Users,DC=company,DC=com)@(LDAP)
15/Apr/2008:10:26:38.779 -0400

Thanks in advance
 
The core functional part is like this.
[tt]
dim s, t, rx, cm, m

set rx=new regexp
with rx
.global=true
.pattern="""([^""]*?)"""
end with

's : input, read-in from the log file line by line
s="152.15.112.88:1755 ssl ""(CN=Hung\,Bart,CN=Users,DC=company,DC=com)@(LDAP)"" ""15/Apr/2008:10:26:38.779 -0400"" v5 connect 13552"

if rx.test(s) then
set cm=rx.execute(s)
for each m in cm
't: output
t= m.submatches(0)
'write the t to the output file; echo for illustration
wscript.echo t
next
set cm=nothing
end if
[/tt]
 
Great!! That worked perfectly. This is what I came up with for the total script.

dim s, t, rx, cm, m

set rx=new regexp
with rx
.global=true
.pattern="""([^""]*?)"""
end with

Const ForReading = 1
Set objFSo = CreateObject("Scripting.FileSystemObject")

strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colFileList = objWMIService.ExecQuery _
("ASSOCIATORS OF {Win32_Directory.Name='D:\SCRIPTING\Aventail\ExtraNet\working'} Where " _
& "ResultClass = CIM_DataFile")

For Each objFileName In colFileList
strName = objFileName.FileName

Set objFile = objFSO.OpenTextFile(objFileName.Name, ForReading)

Set fs = CreateObject("Scripting.FileSystemObject")
Set file = fs.OpenTextFile("D:\SCRIPTING\Aventail\ExtraNet\working\output.csv", 8, True)


Do Until objFile.AtEndOfStream
s = objFile.ReadLine

if rx.test(s) then
set cm=rx.execute(s)
for each m in cm
't: output
t= m.submatches(0)
'write the t to the output file; echo for illustration


file.Write t & ";"


next
set cm=nothing
end if

file.Write vbCrLf
Loop

file.Close


objFile.Close

Next


 
Question for you guys. what would be the pattern if I just wanted to pull the value between the first set of quotes?

The lines in the log file are scructured like this:

152.15.112.88:1755 ssl "(CN=Hung\,Bart,CN=Users,DC=company,DC=com)@(LDAP)" "15/Apr/2008:10:26:38.779 -0400" v5 connect 13552


And I'm looking to grab

(CN=Hung\,Bart,CN=Users,DC=company,DC=com)@(LDAP)


Thanks

 
You can set the global property to false instead if you want to capture only the first match.
[tt] .global=false[/tt]
 
I just wanted to pull the value between the first set of quotes
So, no need of regex:
Code:
...
    Do Until objFile.AtEndOfStream
        s = objFile.ReadLine
        If InStr(s, Chr(34)) > 1 Then
            file.WriteLine Split(s, Chr(34))(1)
        End If
    Loop
...

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top