String 从段落中仅获取tcl/tk中的前两行
作为参考,这是该段落的样子String 从段落中仅获取tcl/tk中的前两行,string,tcl,tk,trim,String,Tcl,Tk,Trim,作为参考,这是该段落的样子 {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.
{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的新手,但我在网上冲浪了很多小时,所以很难理解我。谢谢有几种方法可以做到这一点:
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]
Tcl命令记录在这里:工作Tcl代码如下:
设置文件[打开c:/filename.txt]
设置文件\u设备[读取$file]
设置数据[拆分$file\u设备“\n”]
对于{set count 0}{$count<2}{incr count}{
放入$data
#每件作品都会打印一行。
#split/n用于获取每行的结尾。
#打开命令以给定路径打开文件。
#read命令用于读取打开的文件。
}
关闭$file
打破
我重新格式化文本的方式是否符合您的出发点?此外,数据是在字符串中还是在文件中?您可能需要从以下内容开始:如果我们将“工作”定义为“将文件的全部内容作为行列表打印两次”,则是。而不是切片…@M.D.P