tcl - Tcl如何对文本中的某些单词进行排序并取最后一个
问题描述
我有一个文本并包含
#AA_VERSION = Aa/10.10-d87_1
#AA_VERSION = Aa/10.10-d887_1
#AA_VERSION = Aa/10.10-d138_1
#AA_VERSION = Aa/10.10-d82_1
如何对所有#AA_VERSION =
开头进行排序并打印最后一个?如果文本没有#
开头,如何显示空格或没有版本。
感谢您的热心帮助!
解决方案
假设您已经获得了行内容的列表,您需要做的是遍历该列表并测试有问题的行是否与您的标准匹配;如果是,则将匹配的信息存储在变量中。在循环结束时,变量将包含最后匹配的此类信息。
set version ""
set current ""
foreach line $lines {
if {[regexp {^(#?)AA_VERSION *= *(.+)} $line -> commented info]} {
if {$commented eq "#"} {
set version [string trim $info]
} else {
if {$current ne ""} {
puts stderr "WARNING: multiple current versions"
}
set current [string trim $info]
}
}
}
# All lines scanned; describe what we've found
if {$version eq ""} {
puts "no #AA_VERSION line"
} else {
puts "#AA_VERSION is $version"
}
if {$current eq ""} {
puts "no current AA_VERSION"
} else {
puts "current AA_VERSION is $current"
}
获取文件中所有行列表的经典方法是以下过程:
proc linesOf {filename} {
set f [open $filename]
set data [read $filename]
close $f
return [split $data "\n"]
}
set lines [linesOf "mydata.txt"]
推荐阅读
- git - 版本控制 JetBrains .idea 文件的最新最佳实践是什么?
- css - 带有 CSS 转换的 Safari 渲染错误 - 深度排序/z-index 问题?(在 Chrome 中工作)
- python - 运行发誓过滤器代码时出错 (discord.py)
- javascript - 用 innerHTML 插入 JSX
- beautifulsoup - 修改html标签用“data-src”替换“src”
- r - 如何将多步函数应用于多个数据集,然后将结果组合到新数据集中
- java - 倒计时动画类在某些设备上的工作方式不同
- loops - 如何在ansible的循环中使用任务变量?
- flutter - Flutter - 如何将“FlatButton”转换为“RaisedButton”(切换深色/浅色主题)?+ 文字样式
- flutter - 在 TextFormField 验证器 Flutter 中调用异步函数