如何使用 TCL 脚本从缓冲区打印最后一行的第一个单词



我有缓冲区"showbuffer"。 我想获取缓冲区最后一行的第一个单词。 我怎么做 我使用 Tcl 获取它。我尝试使用拆分

Showbuffer 是

Local Time
 ----------
  Jan 10 14:46:09 2014
  1389345369 secs 905581000 nano secs since the Epoch
Thank you

我习惯于按照命令拆分缓冲区

  set splitBuff [split $showbuffer "n"]

现在我只想要 1389345369 秒905581000自大纪元打印存储在其他缓冲区中以来

的纳秒

缓冲区的最后一行由以下命令获得:

set lastLine [lindex [split $buffer "n"] end]

该行的第一个单词由以下公式获得:

set firstWord [regexp -inline {S+} $lastLine]

处理行时,您可能希望先替换空行:

# Apply the substitution in a loop until it doesn't match
while {[regsub -all {ns*n} $buffer "n" buffer]} {}

一般来说,你会得到以下单词:

set words [regexp -all -inline {S+} $theLine]

然后使用lindexlassign来选择您想要的。

lassign $words seconds - nanosecs
puts "got $seconds and $nanosecs"

如果你想从字符串中提取"M secs N nanosecs form the Epoch",无论它位于何处,你可以:

set showbuffer {Local Time
 ----------
  Jan 10 14:46:09 2014
  1389345369 secs 905581000 nano secs since the Epoch
Thank you}
regexp {d+ secs d+ nano secs since the Epoch} $showbuffer result
puts $result
1389345369 secs 905581000 nano secs since the Epoch

最新更新