There are several different approaches you could take. One would be to use
string last to find the index of last occurrence of a newline (\n), and then
string range to extract the substring from the beginning to just before that point:
[tt]%
set output "AAAAAAA
>
BBBBBBB
>
CCCCCCC
>
ABCD$"
AAAAAAA
BBBBBBB
CCCCCCC
ABCD$
%
[ignore]set index [string last "\n" $output][/ignore]
23
%
[ignore]incr index -1[/ignore]
22
%
[ignore]set desired [string range $output 0 $index][/ignore]
AAAAAAA
BBBBBBB
CCCCCCC[/tt]
Another would be to use regular expressions, if you're comfortable with those. Taking advantage that regular expressions are "greedy" by default (they try to match as many characters as possible), you could get by with this:
[tt]%
regexp {(.*)\n} $output match desired
1
%
puts $desired
AAAAAAA
BBBBBBB
CCCCCCC[/tt]
As you're using Expect, you might be able to incorporate the subpattern directly into your
expect pattern. Something like this:
Code:
expect {
-re {(.*)\r\nABCD\$} {
# If this matches, expect_out(0,string)
# contains all the characters matched,
# and expect_out(1,string) contains the
# characters matching the first
# subpattern (the "(.*)" match).
}
timeout {
# Do whatever timeout stuff you need if
# the pattern doesn't match.
}
eof {
# Handle getting eof notification from
# the spawned process shutting down.
}
Note that when you're using Expect, if you're reading output from a
spawned process, you've also got to take into account that the end-of-line sequence is almost certainly going to be a carriage return-newline sequence (\r\n) instead of just a newline (\n). That means that in the previous examples I showed, you'd have to account for that as well. For example:
[tt]set index [ignore][string last [/ignore]
"\r\n"[ignore] $output]
incr index -1
set desired [string range $output 0 $index][/ignore][/tt]
or
[tt]regexp {(.*)
\r\n} $output match desired[/tt] - Ken Jones, President, ken@avia-training.com
Avia Training and Consulting,
866-TCL-HELP (866-825-4357) US Toll free
415-643-8692 Voice
415-643-8697 Fax