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 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
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., 2024: 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