Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我目前正在制作一个操作多个文件的脚本,并且我正在尝试从变量 ( $pio) 中读取最后一个完整的单词。
$pio
该字符串的长度和字数是可变的,并$pio在函数的开头设置为。
我试图阅读行/变量中的最后一个单词,该行/变量由带有“strip”的空格分隔,但到目前为止我只能按字符数阅读。
是否可以读取字符串的最后一个单词并将其解析为新变量?
您可以拆分字符串,然后获取最后一个索引:
set lastWord [lindex [split $pio] end]
或者,如果您不介意使用一点正则表达式,您可以使用如下内容:
regexp -- {\S+$} $pio lastWord
\S+是一个匹配至少 1 个非空格字符的表达式,并$确保该匹配出现在字符串的末尾。匹配将存储一个名为 的变量lastWord。
\S+
$
lastWord
键盘演示