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 biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to parse results from a command 1

Status
Not open for further replies.

UnderTheRadar

IS-IT--Management
Jun 15, 2005
15
US
I have a command that outputs a plethora of information and I only need one word out if it.

In DOS for example, if I only wanted the IP address of a hostname, I'd write two batch files something like this:

getIP.bat
Code:
@echo off
for /f "tokens=3 delims=: " %%i in ('parseIP') do echo %1 %%i

parseIP.bat
Code:
@echo off
ping -n 1 %1 | find "Reply from"

BTW, I just through this together as an example. I'm sure there's a way to do it with only one script, and I'm not using ping or trying to get an IP address of a hostname.

So anyway, now I'm trying to apply the same logic in VBScript, but I'm not sure how to retrieve only the "token" I need out of these results, not knowing the value of the token of course. I just want the value that comes after the "Reply from".

Here's what I have so far:

Code:
SET objShell = CreateObject("Wscript.shell")
DIM objExec, strResult
SET objExec = objShell.Exec("ping -n 1 localhost")
WHILE objExec.Status <> WshFinished
 'wait until command completes
WEND
strResult = objExec.StdOut.ReadAll

'just to see what's happenin
Wscript.Echo strResult

Any help will be greatly appreciated. I'm sure it's simple, but I searched high and low and I must not be asking the right question.

Thanks in advance.


 
First, I would suggest looking for a way to do whatever your end goal is without resorting to the command line. Failing that, to do what you need to do, look at the Split command in VBS


[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
I specialize in automation so 90% of everything I do involves using the command line. This task in particular would involve writing SQL queries for a set of highly complexed relational databases for which I have no schema, which would take a lot of time that I simply don't have, as this project ends Friday. There's a provided command line utility that already does this, so it would be pointless to research and re-write all of that logic.

Thanks for the "split" tip. I'll look into that.

 
I don't think split() is going to work in this case.

The results of the command I'm issuing list the names of target computers that are reporting to a CA Unicenter Software Delivery Staging Server, and then how many. Something like this.

Code:
COMPUTER1
COMPUTER2
COMPUTER3
...
Number of target computers shown: x

All I want to know is "x", so there's no way of knowing how many computers will be listed or where "x" will end up in the array.

Unless I'm misunderstanding the way split() works?


 
I'm just trying to get the same results in VBScript that I would from this DOS Batch script:

Code:
@for /f "tokens=3 delims=: " %%i in ('ping -n 1 localhost ^| find "Reply from"') do @echo %%i

Anyone?


 
ET objShell = CreateObject("Wscript.shell")
DIM objExec, strResult
SET objExec = objShell.Exec("ping -n 1 localhost")
WHILE objExec.Status <> WshFinished
'wait until command completes
WEND
strResult = objExec.StdOut.ReadAll

'just to see what's happenin
'Wscript.Echo strResult
'assume that Reply From is unique or first hit is good enough

ipos = instr(strResult,"Reply from")
if ipos > 0 then
' find the colon after the ip address
ipostwo = instr(ipos,strResult,":")
' skip over the reply from
ipos = ipos + 11
' return the ip address
strFind = mid(strResult,ipos,ipostwo-ipos)
End If
WScript.Echo strFind
'
' another example.... no colon look for next space
ipos = instr(strResult,"bytes=")
if ipos > 0 then
' find the next space
ipostwo = instr(ipos,strResult," ")
' skip over the reply from
ipos = ipos + 6
' return the number
strFind = mid(strResult,ipos,ipostwo-ipos)
End If
WScript.Echo strFind
 
Split would work. You would split on vbCrLf then you would have an array of the lines from the stdOut. Then you would process each of these lines splitting again to get the information that you want from each line.

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
I want to thank everyone who replied for your help. I tested out all of your methods and they all worked, so I gave each one of you a star.

I actually got the answer I was looking for in the MSDN newsgroup, the key word being "RegEx". It was also mentioned in the link that longhair posted.

Hey, that sounds like a good title for a book or something. It's got a nice ring to it. The Link That Longhair Posted...

In case anyone is interested, this will give you the result for which I was looking.

Code:
Dim RegEx, WSH
Set RegEx = New RegExp
Set WSH = CreateObject("WScript.Shell")

Set oExec = WSH.Exec("ping.exe -n 1 localhost")
RegEx.Pattern = "^Reply from ([^:]+).*$"

Do Until oExec.Status = 0
 WScript.Sleep 100
Loop

Do Until oExec.StdOut.AtEndOfStream
 Line = oExec.StdOut.ReadLine
 If RegEx.Test(Line) Then IP = RegEx.Replace(Line, "$1")
Loop

WScript.Echo IP

Thanks again everyone.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top