Problems using Expect to parse a file
- From: Tom McEwan <tnm23@xxxxxxxxxxxxxxxxxxxxx>
- Date: Sun, 07 Sep 2008 19:04:26 +0100
Sorry if this is a well known problem; I have tried to find any pre-existing solution searching the web and buying "Exploring Expect", but not got anywhere.
I'm trying to write a script to automate a program called DeltaE, a powerful thermoacoustic design tool (with, unfortunately, a ghastly menu-only interface), and to extract performance data from the simulations it runs by parsing through its output files. For reference, I'm using Linux AMD64, with the DeltaE.exe program running under Wine.
I've just about got the hang of driving the menus in Expect, but I'm having real trouble extracting the performance data.
My first attempt was to load the (plain text) output file as a process, as Expect is supposed to be capable of doing, then running the appropriate expect commands on its spawn_id. The trouble is, all this does is load the whole file into the expect buffers in one go, which would be OK on its own, but then of course it detects the eof and immediately closes the process again, and so all the subsequent expect commands acting on the buffer seem to fail because they don't have a valid process id to interact with. Is there any possibility of working around this problem? The relevant code for that attempt looks like this (apologies if my particular indenting style is annoying):
set fidOutput [open "data/$n.out" r]
spawn -open $fidOutput
for {set m 1} {m <= 3} {incr m} \
{
expect "HX" etc...
}
Where $n is the numeric filename and "HX" is the marker preceding the first of three pieces of data to extract.
Since I couldn't figure out a way to fix that directly, I decided to follow the Expect philosophy of running the program the way a human would do and spawn a program to read the file. cat and more couldn't be used, because they dump all the data and immediately terminate, causing the same "loss of process" problem as before. Less, on the other hand, remains open upon reaching the end of a file until it receives the "q" command, so I tried writing a script to step through the file using that, as follows:
spawn less "-c data/$n.out"
set pidLess $spawn_id
send "\r"
for {set m 1} {$m <= 3} {incr m} \
{
set bHX 0
while { !($bHX) } \
{
expect \
{
"HX" {set bHX 1 ; etc}
"\n\:" {send "\r"
}
}
}
Which should, I think, cycle through all the pages of the file displayed by Less until finding an instance of "HX", then extract the following data. The "-c" parameter makes Less print each page top to bottom, so that the prompt, which I am using to signify the end of the page, is not seen by expect before the page itself is actually displayed. The trouble now is, although manually executing the command "less -c data/1.out" at a tty functions just as you would expect it to, when Expect sends the exact same command as part of the spawn process, and running the script with exp_internal true, apparently none of the data sent to the display by less when started normally is being seen by Expect - all it seems to get is "No tags file\r\n", followed by eof and closure of Less - I'm afraid I haven't a clue what the "No tags file" statement means.
Can anyone help me with any of this mess? The whole script is pretty large and sprawling so I've only posted snippets, but I'll put up the whole thing if anyone needs it.
Thanks.
Tom
.
- Follow-Ups:
- Re: Problems using Expect to parse a file
- From: Cameron Laird
- Re: Problems using Expect to parse a file
- From: Uwe Klein
- Re: Problems using Expect to parse a file
- Prev by Date: Re: TclX loop slow in the default case
- Next by Date: Re: Problems using Expect to parse a file
- Previous by thread: Re: TclX loop slow in the default case
- Next by thread: Re: Problems using Expect to parse a file
- Index(es):
Relevant Pages
|