Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
For 2021 - online Python 3 training - see ((here)).

Our plans were to retire in summer 2020 and see the world, but Coronavirus has lead us into a lot of lockdown programming in Python 3 and PHP 7.
We can now offer tailored online training - small groups, real tutors - works really well for groups of 4 to 14 delegates. Anywhere in the world; course language English.

Please ask about private 'maintenance' training for Python 2, Tcl, Perl, PHP, Lua, etc.
Char "^M" at end of each line in output file

Posted by Kan (Kan), 24 November 2005
I've prepared a expect script on a Linux box to telnet to a SUNOS box. In the telnet session, I ping to 2 remote host to check connectivity and output the ping result to a file.

However, when I use vi to view the file, there are strange char "^M" at the end of each line. How to prevent "^M" from writing into the output file?

Here is my script:
#!/usr/bin/expect
set timeout 300
set hostname IPAddress    
set username abc
set password pw
spawn telnet $hostname
expect "login: "
send "$username\r"
expect "Password:"
send "$password\r"
send "\r"
expect "bash$ "
set ret [open result.txt w]
send "ping 1.1.1.1\r"
send "ping 1.1.1.2\r"
send "uname\r"
expect "SunOS"
puts $ret "$expect_out(buffer)"
close $ret
exit

Here is the result.txt view in vi:
^M
bash$ ping 1.1.1.1^M
1.1.1.1 is alive^M
bash$ ping 1.1.1.2^M
1.1.1.2 is alive^M
bash$ uname^M
SunOS

Posted by admin (Graham Ellis), 24 November 2005
At the end of each line you've got a carriage return - control M - character.   You'll see it in Tcl strings represented as \r.  Terminal output has a carriage return, line feed sequence on it as do Windows files - that's ^M^J or \r\n.  

The string trim command can be used to get rid of leading and trailing white space.

Posted by Kan (Kan), 25 November 2005
Thanks Graham.

It works after I string trim each line in $expect_out(buffer) before I write to the output file as follow:

set buf $expect_out(buffer)
foreach line [split $buf "\n"] {
   set line [string trim $line]
    puts $ret $line
}



This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2021: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho