作为参考,这是段落的样子。
{180319 arun S B} first set of chars. first chars.
{180316 yyay S B} second set of chars. second line.
{180314 ramaw S B} third line. third line. third line.
{180309 jfds S B} fouth line
{180221 shrbsd S B} fifth line.fith line part 2.
{180214 shrbs S B} sixth line.
在这里我需要提取前两行。像
{180319 arun S B} first set of chars. first chars.
{180316 yyay S B} second set of chars. second line.
我不知道如何在tcl中执行此操作。你能建议我这样做吗?虽然我是tcl的新手,但我已经上网了好几个小时,所以很难理解我。感谢
答案 0 :(得分:1)
有几种方法可以做到这一点:
将段落拆分为行并加入前两行:
set lines [split $para \n]
set first2 [lrange $lines 0 1]
set wanted [join $first2 \n]
# or
set wanted [join [lrange [split $para \n] 0 1] \n]
找到第二个换行符的位置,并从段落开头到该位置取字符
set firstnewline [string first \n $para]
set secondnewline [string first \n $para $firstnewline+1]
set wanted [string range $para 0 $secondnewline-1]
您还可以使用
获取第二个换行符索引set secondnewline [lindex [regexp -all -inline -indices \n $para] 1 0]
答案 1 :(得分:0)
工作TCL代码将遵循:
set file [open c:/filename.txt ]
set file_device [read $file]
set data [split $file_device "\n"]
for {set count 0} {$count < 2} {incr count} {
puts $data
# for every iterartion one line will be printed.
# split /n is use for getting the end of each line.
# open command open the file at given path.
# read command is use to read the open file.
}
close $file
break
这肯定会有用。