Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

stripping newline character in expect

809 views
Skip to first unread message

thh...@charter.net

unread,
Nov 3, 2002, 6:06:35 PM11/3/02
to
Can someone tell me how to strip the newline character . I am pretty
new to expect and need somthing like chomp in perl . Here is my code.
if [ catch {open "/tmp/file.txt" "r"} infile ] {
puts "Error Opening file.txt"
exit
}
while 1 {
if {[gets $infile line] == -1 } break
set ouput [ split $line "\t" ]
set user [ lindex $output 0 ]
set password [ lindex $output 1 ]
puts "$user $password"
}


My input looks like
user password
user1 password1

my question is how do I make password be set to the password and not
to password\r ??
Any help is appretiated.

Bruce Hartweg

unread,
Nov 3, 2002, 10:27:33 PM11/3/02
to


hmmmm, gets should strip the newline automatically, is that
tthe actual, complete code? does the translation for
the channel ever get set explicitly?

anyway, [string trim $str] will remove all extra whitespace
from each end of a string (se also string trimleft,
string trimright)


Bruce

Glenn Jackman

unread,
Nov 4, 2002, 10:34:50 AM11/4/02
to
thh...@charter.net <thh...@charter.net> wrote:
> Can someone tell me how to strip the newline character . I am pretty
> new to expect and need somthing like chomp in perl . Here is my code.
...

> while 1 {
> if {[gets $infile line] == -1 } break
> set ouput [ split $line "\t" ]
> set user [ lindex $output 0 ]
> set password [ lindex $output 1 ]
> puts "$user $password"
> }
...
> my question is how do I make password be set to the password and not
> to password\r ??

The password should not contain any trailing cf or lf characters. From
the [gets] man page:

This command reads the next line from channelId, returns everything
in the line up to (but not including) the end-of-line character(s),
and discards the end-of-line character(s).

If you have a text file with "crlf" terminated lines, and only the lf is
discarded, you might want to read up on [fconfigure -translation] to
have tcl recognize the end-of-line characters for you. see
http://wiki.tcl.tk/fconfigure

If you're coming from perl, you may not realize that the tcl [puts]
appends a trailing newline for you.

--
Glenn Jackman
gle...@ncf.ca

0 new messages