LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Is it possible to control VI with tcl and expect ? (https://www.linuxquestions.org/questions/programming-9/is-it-possible-to-control-vi-with-tcl-and-expect-867013/)

Stuart07 03-07-2011 12:05 PM

Is it possible to control VI with tcl and expect ?
 
I'm using expect to log into a remote server and run a script. I want to able to have expect edit the output from that script, on the remote server, in VI.

Has anyone ever done this before, it seems as though the expect interpreter has nothing to expect once VI is started, the file pointer starts at the beginning of the line, and expect usually reads the characters before the pointer.

spawn ssh user1\@$remote_server

expect "password: "

send "$password\r"

expect "$ "

send "cd /data/home/user1/bin \r"

expect "$ "

send "/data/bin/script1.pl > report1.txt\r"

expect "$ "

send "vi /data/bin/report1.txt\r"

###This is where I get lost on what to have it expect
###Can you expect nothing ("") ?
###Can you have multiple "send" commands without expect
####in between?

expect ""

send {:g/search string/s/1.*0//}

expect ""

send "\r"

expect ""

send {:v/./d}




Any thoughts at all are appreciated!
Thanks,
Mark

macemoneta 03-07-2011 01:34 PM

Use sed instead of vi. It's intended for command line editing.

Stuart07 03-08-2011 08:30 AM

Thanks.

After further testing, figured out you can have multiple send commands without an expect between. To accommodate the delay I used the sleep command. Seems to work although sed might be a more appropriate solution.

Code:

send    ":"

        sleep 1

        send    "v/./d\r"

        sleep 1

        send    ":"

        sleep 1

        send    { %s/[a-z].*4//g }

        send    "\r"

        sleep 1

        send    ":"

        sleep 1

        send    { %s/\s\+$// }

        send    "\r"

        sleep 1

        send    ":"

        sleep 1

        send    "wq!"



All times are GMT -5. The time now is 03:38 AM.