如何使用tcl/expect替换文件中的字符串 [英] how to replace a string in a file using tcl /expect

查看:705
本文介绍了如何使用tcl/expect替换文件中的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将输出重定向到文本文件.它按预期工作.但是我想用不同的字符串替换文本文件中的单词.我试过了,但是没有用.你能帮我这个忙吗?

I was trying to redirect the output to a text file. It works as expected. However i would like to replace a word in the text file with a different string. I tried but it didn't work. Could you please help me with this.

set output [open /scripts/cisco/output4 "a+"]

for {set time 0} {$time < 3} {incr time} {
    for {set times 0} {$times < 3} {incr times} {
        log_user 0
        exp_send "vmstat -n 2 5\r"

        puts $output $expect_out(buffer) 
        expect "debugshell"
    }
    sleep 20
}

# i used regsub to replace the the string debugshell with  shell
regsub -all debugshell $output shell output4
close $output

推荐答案

要更改正在写入的文件中的字符串,需要先在内存中对其进行更改,然后再将其写入. (或者,您可以将其写出来,再次读入,进行更改,然后写出更改的版本.这完全可以解决所有问题!)鉴于这是本来打算从程序中输出的文本一个人读书,不会那么多.最多几个兆字节.这意味着我们可以轻松地将其存储在内存中,并在最后进行写操作.

In order to change a string in a file that you are writing, you need to change it in memory before you write it out. (Or you could write it out, read it in again, make the change, and write the changed version out. Which is totally making a meal out of it all!) Given that this is text output from a program that was originally intended for a person to read, it won't be that much. A few megabytes at most. That means that we can easily accumulate it in memory and do the write at the end.

set accumulate ""
for {set time 0} {$time < 3} {incr time} {
    for {set times 0} {$times < 3} {incr times} {
        log_user 0
        exp_send "vmstat -n 2 5\r"
        append accumulate $expect_out(buffer) "\n"
        expect "debugshell"
    }
    sleep 20
}

regsub -all debugshell $accumulate shell accumulate_mapped
set output [open /scripts/cisco/output4 a+]
puts $output $accumulate_mapped
close $output

好的,因为我们正在讨论将转换应用于每行(-all放弃了游戏),所以我们可以随时进行转换.在这种情况下,我们可以这样做:

OK, since we're talking about applying the transformation to each line (that -all gives the game away) we can do the transformation as we go. In that case, we might do this:

set output [open /scripts/cisco/output4 a+]

for {set time 0} {$time < 3} {incr time} {
    for {set times 0} {$times < 3} {incr times} {
        log_user 0
        exp_send "vmstat -n 2 5\r"
        regsub -all debugshell $expect_out(buffer) shell mapped
        puts $output $mapped
        expect "debugshell"
    }
    sleep 20
}

close $output

由于Expect捕获缓冲区还捕获换行符,因此您可以考虑使用puts -nonewline而不是puts. 很可能该代码可能无法完全满足您的期望;一般来说,您只应该在expect匹配某项后立即查看$expect_out(buffer).

Because the Expect capture buffer also traps newlines, you might consider using puts -nonewline instead of puts. It's also quite possible that this code won't do exactly what you expect; generally speaking, you're only really supposed to look at $expect_out(buffer) straight after an expect matches something.

这篇关于如何使用tcl/expect替换文件中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆