Expect 字符串连接选项卡

Tab on Expect String concatenation

我是 Expect 的新手,但我无法解决我在编写的日志记录监控脚本中遇到的问题。

我花了几个小时谷歌搜索为什么我不能让它工作:

puts $redirect [concat "${time}\t" "${context}\t" "$id\t" "${eventtype}" "${eventstatus}\t" "${eventcontext}" ] 

\t 字符(即使与其他 \chars 也不起作用)没有显示。不管我把它放在哪里,我都尝试过不同的东西:

puts $redirect [concat "${time}" "\t" "${context}"  [...] ]
puts $redirect [concat "${time}\t" "${context}" [...] ]
puts $redirect [concat "${time}" "\t${context}" [...] ]
puts $redirect [concat "${time}" \t "${context}" [...] ]

*重定向是 set redirect [open $logfile a]

*其中 [...] 是我以相同方式连接的其他字符串。

来自http://tcl.tk/man/tcl8.5/TclCmd/Tcl.htm#M10

[5] Argument expansion. If a word starts with the string “{}” followed by a non-whitespace character, then the leading “{}” is removed and the rest of the word is parsed and substituted as any other word. After substitution, the word is parsed as a list (without command or variable substitutions; backslash substitutions are performed as is normal for a list and individual internal words may be surrounded by either braces or double-quote characters), and its words are added to the command being substituted. For instance, “cmd a {}{b [c]} d {}{$e f "g h"}” is equivalent to “cmd a b {[c]} d {$e} f "g h"”.

[6] Braces. If the first character of a word is an open brace (“{”) and rule [5] does not apply, then the word is terminated by the matching close brace (“}”). Braces nest within the word: for each additional open brace there must be an additional close brace (however, if an open brace or close brace within the word is quoted with a backslash then it is not counted in locating the matching close brace). No substitutions are performed on the characters between the braces except for backslash-newline substitutions described below, nor do semi-colons, newlines, close brackets, or white space receive any special interpretation. The word will consist of exactly the characters between the outer braces, not including the braces themselves.

讽刺的是,我可以让它工作:

puts $redirect [concat "${time}\n" "-\t${context}" [...] ]

如果我在 TAB 之前放一个字符,它可以工作,但我不能使用它。

Ex 输出:2016-06-01 15:43:12 - macro

想要的输出:2016-06-01 15:43:12 macro

我试过用 append 构建字符串,但由于最大缓冲区字符,它好像正在吃掉一些字符串,这可能吗?

我错过了什么吗? 谢谢指教。

这就是 concat 所做的。它吃掉空格。

来自 concat 的文档:

This command joins each of its arguments together with spaces after trimming leading and trailing white-space from each of them. If all the arguments are lists, this has the same effect as concatenating them into a single list. It permits any number of arguments; if no args are supplied, the result is an empty string.

@Etan 告诉你为什么它不适合你。

另一种编码方式是使用format

puts $redirect [format "%s\t%s\t%s\t%s%s\t%s" $time $context $id $eventtype $eventstatus $eventcontext]