expect, so you have no control over which parts of the remote host's output are captured. You can redirect the entire output to a file (and process the file later with
perl, or whatever to extract only what you need) or pipe it directly into
awk (or whatever) before writing to a file or storing it in a shell variable (with
$() command substitution).
However, you'd almost certainly be better off writing your script in
perl (and use either the
Expect.pm module or
Net::IMAP) or in
imaplib2 library), or in some other language with an
expect-like library or IMAP client library.
That would allow you to connect to the relevant port (IMAP, by the look of it) on the remote host, issue whatever commands you need, wait for responses, and do whatever you need with the response (including saving some of the responses to a file)
I've added the
/expect tag to your question - click on it to get a list of
expect-related questions and answers on this site.