need to pull specific line out of Variable  
Author Message
scmhogg





PostPosted: Mon Mar 26 10:08:42 CDT 2007 Top

VB Scripts >> need to pull specific line out of Variable

I have a script that creates a Variable "output" that contains the
following

Username: XXXXXX
Password:


WELCOME TO THE TEST ENVIRONMENT ROUTER


xxxxxx-TEST-BRANCH#ping 1.1.1.1

Type escape sequence to abort.
Sending 5, 100-byte ICMP Echos to 1.1.1.1, timeout is 2 seconds:
.....
Success rate is 0 percent (0/5)
xxxxxx-TEST-BRANCH#ping 1.1.1.1

Type escape sequence to abort.
Sending 5, 100-byte ICMP Echos to 1.1.1.1, timeout is 2 seconds:
...!!
Success rate is 40 percent (2/5)
xxxxxx-TEST-BRANCH#

the above info is stored in memory in the variable OUTPUT. I need to
be able to look for the 2 "success rate" lines and place them in 2
seperate variables.
Success1 and Success2

help.

Visual Studio341  
 
 
ekkehard





PostPosted: Mon Mar 26 10:08:42 CDT 2007 Top

VB Scripts >> need to pull specific line out of Variable DrkNite schrieb:
> I have a script that creates a Variable "output" that contains the
> following
>
> Username: XXXXXX
> Password:
>
>
> WELCOME TO THE TEST ENVIRONMENT ROUTER
>
>
> xxxxxx-TEST-BRANCH#ping 1.1.1.1
>
> Type escape sequence to abort.
> Sending 5, 100-byte ICMP Echos to 1.1.1.1, timeout is 2 seconds:
> .....
> Success rate is 0 percent (0/5)
> xxxxxx-TEST-BRANCH#ping 1.1.1.1
>
> Type escape sequence to abort.
> Sending 5, 100-byte ICMP Echos to 1.1.1.1, timeout is 2 seconds:
> ...!!
> Success rate is 40 percent (2/5)
> xxxxxx-TEST-BRANCH#
>
> the above info is stored in memory in the variable OUTPUT. I need to
> be able to look for the 2 "success rate" lines and place them in 2
> seperate variables.
> Success1 and Success2
>
> help.

Try:

Dim sTest : sTest = Join( Array( _
"Username: XXXXXX" _
, "Password: " _
, "" _
... etc ...
, "....." _
, "Success rate is 0 percent (0/5)" _
, "xxxxxx-TEST-BRANCH#ping 1.1.1.1" _
... etc ...
, "...!!" _
, "Success rate is 40 percent (2/5)" _
, "xxxxxx-TEST-BRANCH#" _
), vbCrLf )
Dim rePiPro : Set rePiPro = New RegExp
' Success rate is 0 percent (0 /5)
rePiPro.Pattern = "Success rate is \d+ percent \(\d+/\d+\)"
rePiPro.Global = True
Dim oMTS : Set oMTS = rePiPro.Execute( sTest )
Dim oMT
For Each oMT In oMTS
WScript.Echo ">" + oMT.Value + "<"
Next
 
 
Bob





PostPosted: Mon Mar 26 10:08:15 CDT 2007 Top

VB Scripts >> need to pull specific line out of Variable
> I have a script that creates a Variable "output" that contains the
> following
>
> Username: XXXXXX
> Password:
>
>
> WELCOME TO THE TEST ENVIRONMENT ROUTER
>
>
> xxxxxx-TEST-BRANCH#ping 1.1.1.1
>
> Type escape sequence to abort.
> Sending 5, 100-byte ICMP Echos to 1.1.1.1, timeout is 2 seconds:
> .....
> Success rate is 0 percent (0/5)
> xxxxxx-TEST-BRANCH#ping 1.1.1.1
>
> Type escape sequence to abort.
> Sending 5, 100-byte ICMP Echos to 1.1.1.1, timeout is 2 seconds:
> ...!!
> Success rate is 40 percent (2/5)
> xxxxxx-TEST-BRANCH#
>
> the above info is stored in memory in the variable OUTPUT. I need to
> be able to look for the 2 "success rate" lines and place them in 2
> seperate variables.
> Success1 and Success2
>
> help.
I'm sure there's a really incomprehensible regular expression that does
this with a single command, but I' will let somebody else handle posting
that. Here is a non-regexp way:

First of all, you need to determine the character being used to cause
the line breaks. It is usually a combination of two characters, denoted
by the vbCrLf constant, but it could be either vbCr or vbLf. Let's
assume it is the usual vbCrLf. The idea is to use the Break function to
break up the string based on the vbCrLf characters, populating an array
with the string fragments:

dim ar
ar=Break(OUTPUT,vbCrLf)

Now simply loop through the array looking for the string "Success rate"
in the array elements:

for each s in ar
if instr(lcase(s),"success rate")> 0 then
if len(success1) = 0 then
success1=s
else
success2=s
exit for
end if
end if
next



--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.


 
 
mayayana





PostPosted: Mon Mar 26 10:11:25 CDT 2007 Top

VB Scripts >> need to pull specific line out of Variable There are a lot of ways you can do it.
This one should be fairly quick and easy:

(Note there's no error checking added here.)

Dim Success1, Success2, A1, i, Boo, s
Boo = False
A1 = Split(output, vbCrLf)
For i = 0 to ubound(A1)
s = A1(i)
If instr(1, s, "success", 1) > 0 Then
If Boo = True Then
Success2 = A1(i)
Else
Success1 = A1(i)
End If
Boo = True
End If
Next


> I have a script that creates a Variable "output" that contains the
> following
>
> Username: XXXXXX
> Password:
>
>
> WELCOME TO THE TEST ENVIRONMENT ROUTER
>
>
> xxxxxx-TEST-BRANCH#ping 1.1.1.1
>
> Type escape sequence to abort.
> Sending 5, 100-byte ICMP Echos to 1.1.1.1, timeout is 2 seconds:
> .....
> Success rate is 0 percent (0/5)
> xxxxxx-TEST-BRANCH#ping 1.1.1.1
>
> Type escape sequence to abort.
> Sending 5, 100-byte ICMP Echos to 1.1.1.1, timeout is 2 seconds:
> ...!!
> Success rate is 40 percent (2/5)
> xxxxxx-TEST-BRANCH#
>
> the above info is stored in memory in the variable OUTPUT. I need to
> be able to look for the 2 "success rate" lines and place them in 2
> seperate variables.
> Success1 and Success2
>
> help.


 
 
Bob





PostPosted: Mon Mar 26 10:20:42 CDT 2007 Top

VB Scripts >> need to pull specific line out of Variable
> Let's assume it is the usual vbCrLf. The idea is to use the Break
> function to break up the string based on the vbCrLf characters,
> populating an array with the string fragments:
>
> dim ar
> ar=Break(OUTPUT,vbCrLf)
>

Oops! Wrong language. Split(), not Break()
--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.


 
 
Richard





PostPosted: Mon Mar 26 10:17:11 CDT 2007 Top

VB Scripts >> need to pull specific line out of Variable



>> I have a script that creates a Variable "output" that contains the
>> following
>>
>> Username: XXXXXX
>> Password:
>>
>>
>> WELCOME TO THE TEST ENVIRONMENT ROUTER
>>
>>
>> xxxxxx-TEST-BRANCH#ping 1.1.1.1
>>
>> Type escape sequence to abort.
>> Sending 5, 100-byte ICMP Echos to 1.1.1.1, timeout is 2 seconds:
>> .....
>> Success rate is 0 percent (0/5)
>> xxxxxx-TEST-BRANCH#ping 1.1.1.1
>>
>> Type escape sequence to abort.
>> Sending 5, 100-byte ICMP Echos to 1.1.1.1, timeout is 2 seconds:
>> ...!!
>> Success rate is 40 percent (2/5)
>> xxxxxx-TEST-BRANCH#
>>
>> the above info is stored in memory in the variable OUTPUT. I need to
>> be able to look for the 2 "success rate" lines and place them in 2
>> seperate variables.
>> Success1 and Success2
>>
>> help.
> I'm sure there's a really incomprehensible regular expression that does
> this with a single command, but I' will let somebody else handle posting
> that. Here is a non-regexp way:
>
> First of all, you need to determine the character being used to cause
> the line breaks. It is usually a combination of two characters, denoted
> by the vbCrLf constant, but it could be either vbCr or vbLf. Let's
> assume it is the usual vbCrLf. The idea is to use the Break function to
> break up the string based on the vbCrLf characters, populating an array
> with the string fragments:
>
> dim ar
> ar=Break(OUTPUT,vbCrLf)
>
> Now simply loop through the array looking for the string "Success rate"
> in the array elements:
>
> for each s in ar
> if instr(lcase(s),"success rate")> 0 then
> if len(success1) = 0 then
> success1=s
> else
> success2=s
> exit for
> end if
> end if
> next
>
>
>
> --
> Microsoft MVP -- ASP/ASP.NET
> Please reply to the newsgroup. The email account listed in my From
> header is my spam trap, so I don't check it very often. You will get a
> quicker response by posting to the newsgroup.
>
>

Don't you mean Split in place of Break?

ar = Split(OUTPUT, vbCrLf)

--
Richard Mueller
Microsoft MVP Scripting and ADSI
Hilltop Lab - http://www.rlmueller.net
--


 
 
DrkNite





PostPosted: Mon Mar 26 11:02:27 CDT 2007 Top

VB Scripts >> need to pull specific line out of Variable On Mon, 26 Mar 2007 10:17:11 -0500, "Richard Mueller [MVP]"


>ar = Split(OUTPUT, vbCrLf)


Thanks Richard
Thanks Bob

worked as just how i wanted it to.
thanks for the help