我有一个小的tcl脚本,可以从文件输出文本
test.txt中的是文本:
---------------------------[list]-----------------------------------------
00.01 aaaa
00.02 bbbbbbbbbbbbbbbbbb
--------------------------------------------------------------------------
我的tcl脚本
bind pub - !textlist pub:textlist
proc pub:textlist {nick uhost hand chan args} {
set file [open /home/test.txt]
set input [read $file]
set lines [split $input "\n"]
foreach line $lines {
putnow "PRIVMSG $chan :$line"
}
}
输出现在是:
<@t> !textlist
<@test> ---------------------------[list]-----------------------------------------
<@test> 00.01 aaaa
<@test> 00.02 bbbbbbbbbbbbbbbbbb
<@test> --------------------------------------------------------------------------
<@test> :
如何删除通道输出中的last:?在文件test.txt中不是最后一行之后的新行-----
致谢
答案 0 :(得分:2)
read
命令实际上具有在文件末尾丢弃尾随换行符的选项。因此,只需将“读取”行更改为:
set input [read -nonewline $file]
答案 1 :(得分:1)
当文件包含两个换行符时:
aaaa
bbbb
使用换行符分隔符的split
操作将拆分组件
分为三个部分。最后一部分是空的。
您可以在拆分操作之前删除换行符:
regsub "\n$" $input {} input
set lines [split $input \n]
或者您可以更改文件的处理方式:
set fh [open myfile.txt]
while { [gets $fh line] >= 0 } {
puts $line
}
close $fh
(另外:您的示例代码在读取文件后没有关闭文件)。