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

use expect for file contents

Status
Not open for further replies.

hf24601

Programmer
Apr 13, 2010
1
US
I would like to use expect to parse through the contents of a file, something like this:

spawn cat log/mylog.txt
expect {
-re "some_expr" { ...do something... }
default { ...do something else... }

Note that I am simply reading a file, so there is nothing really interactive going on. Even so, this works most of the time, but sometimes I think that expect gets ahead of itself and tries to read the file contents before they are ready, and I get failed tests.

Is there some convenient way to use the spawn/expect sequence for reading through a file? Is there any easy way to force expect to wait until the spawn is completed?

Spawn/expect are designed for interactive use. Since I am mostly non-interactive, is there a better way to do this than the approach I am taking?

Thanks in advance.
 
set file log/mylog.txt
if {[catch {open $file} input] == 1} {
puts "File not found!"

} else {
while { [gets $input line] != -1 } {

puts $line
if {[regexp "some_expr" $line] == 1 } {
puts "some_expr found in the file!"
}
}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top