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

Shell Command StdOut 1

Status
Not open for further replies.

johnpayback

IS-IT--Management
Oct 2, 2006
110
US
I need to execute a grep command on a text file and display the output to an ASP file. I was not sure if I should ask in this forum or the ASP forum. How can I run a command and display it's results to the screen?

JP
 
This is very helpful. The problem seems to be that the StdOut cannot support large amounts of data. The way that I'm using it is I am running a GREP command on a very large text file to find keywords in the text file. Then I am displaying to the screen. It does seem to work if I run from my desktop but when I try to use it in ASP it timesout. If you have a suggestion on how to keep this from happening I would appreciate it. Below is the code I'm testing right now.


strtextToSearch is the keyword entered into the ASP page FolderToSearch is the path to the file that I am running the grep command on

Code:
strtextToSearch = Request("TextToSearch")
FolderToSearch = "\folderto\pathto\Logfile.txt"

strcmd = "%comspec% /Q /C find | grep -i " & Chr(34) & strTexttoSearch & Chr(34) & " " & FolderToSearch

oWSH.Run strcmd, 0, True
strPResult = strCmd.StdOut.ReadAll()
 
response.write ".<br>" & replace strPResult,vbLf,"<br><hr>")

I get a runtime error which seems to me as being a timeout because running this on the desktop from a .vbs file works perfectly fine.

I've also successfully did this same thing piping the output of the command to another text file and then displaying the contents of that text file to the screen. I'm just not sure why using the StdOut will not allow me to do this? Thanks.

JP
 
By the way, I've replaced the normal FIND and GREP commands on the Windows XP box with UNIX commands for Windows. Sorry...I should have explained that before. It doesn't seem to make a difference either way from the desktop. Thanks.

JP
 
Try something like this or variation of it.
[tt]
strtextToSearch = Request("TextToSearch")
FolderToSearch = "\folderto\pathto\Logfile.txt"

strcmd = "%comspec% /Q /C find | grep -i " & Chr(34) & strTexttoSearch & Chr(34) & " " & FolderToSearch
set oexec=oWSH.exec(strcmd)
response.write ".<br />"
do while not oexec.StdOut.AtEndOfStream
strPResult=replace(oexec.StdOut.Read(1024), vbLF, "<br /><hr />"
response.flush
loop
set oexec=nothing
set oWSH=nothing
[/tt]
 
tsuji, that works great. Is there an easy way to allow multiple keywords in a search such as this? If so, can you share? Either way you get a star. Thanks man.

JP
 
To seach multiple keyword depends on the support of regular expression of various flavor of grep on different platform. You can use "|" (or operator), in particular, you might have to escape it "\|" and then enclose the whole search pattern by chr(34) as you've done. Try something like this.
[tt]
s1=request("texttosearch_1")
s2=request("texttosearch_2")
'etc in principle finite number...
strTextToSearch=s1 & "\|" & s2
'continue the same
[/tt]
ps. I had a typo of mismatch parentheses in the replace() line I posted. I'm sure you have added missing closing ")" at the end.
 
After note
Just to make the record straight... upon re-read what I posted, I missed out another latent line and forgotten to type out. Here is the re-list of the do loop. (I'm sure the op has got the idea already.)
[tt]
do while not oexec.StdOut.AtEndOfStream
strPResult=replace(oexec.StdOut.Read(1024), vbLF, "<br /><hr />"[blue])[/blue]
[blue]response.write strPResult[/blue]
response.flush
loop
[/tt]
 
Sorry it took me so long to get back to you. I did see the typos and they weren't a problem. I've been working on the multiple keywords and it seems to run fine except it can't finish running...maybe because the files are so large. Below is the error that I get. I'm trying 6 keywords.

Error Type:
Active Server Pages, ASP 0115 (0x80004005)
A trappable error (E06D7363) occurred in an external object. The script cannot continue running.

Thanks again

JP
 
Okay I was able to get it working but it doesn't search for every item. It will only search for the last item in the grep command. Any ideas on how to have it search for lines containing all or nothing or even lines that contain any of the searched items?

JP
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top