use*_*071 1 regex bash foreach ls tcl
如果我有一个包含以下文件的文件夹:
hello-version-1-090.txt
hello-awesome-well-091.txt
goodday-087.txt
hellooo-874.txt
hello_476.txt
hello_094.txt
Run Code Online (Sandbox Code Playgroud)
如何使用 tcl 搜索包含以下术语的文件:“hello”和“091”。
可能的解决方案:获取ls -l文件夹中 an 的输出,将其拆分'\n',然后foreach在每一行上运行 a并使用正则表达式匹配条件。但是如何ls -l在文件夹中运行并使用 tcl 记录其保存内容(文件名)?
使用glob,您可以应用模式并获得符合我们标准的文件名列表。
puts [ exec ls -l ]; #Just printing the 'ls -l' output
set myfiles [ glob -nocomplain hello*091*.txt ]
if {[llength $myfiles]!=0} {
puts "Following files matched your pattern : "
foreach fname $myfiles {
puts $fname
}
} else {
puts "No files matched your pattern"
}
Run Code Online (Sandbox Code Playgroud)
使用的原因-nocomplain是如果没有与我们的搜索模式匹配的文件,则允许返回空列表而不会出错。
输出
sh-4.2# tclsh main.tcl
total 4
-rw-r--r-- 1 root root 0 Mar 4 15:23 goodday-087.txt
-rw-r--r-- 1 root root 0 Mar 4 15:23 hello-awesome-well-091.txt
-rw-r--r-- 1 root root 0 Mar 4 15:23 hello-version-1-090.txt
-rw-r--r-- 1 root root 0 Mar 4 15:23 hello_094.txt
-rw-r--r-- 1 root root 0 Mar 4 15:23 hello_476.txt
-rw-r--r-- 1 root root 0 Mar 4 15:23 hellooo-874.txt
-rw-r--r-- 1 root root 262 Mar 4 15:24 main.tcl
Following files matched your pattern :
hello-awesome-well-091.txt
Run Code Online (Sandbox Code Playgroud)
顺便说一下,关于如何保存ls -l输出的查询,只需将输出保存到变量即可。
set result [ exec ls -l ]
Run Code Online (Sandbox Code Playgroud)
然后使用result变量,您可以regexp像您提到的那样通过逐行循环来应用。
但是,我希望使用glob会是一个更好的方法。
参考:glob
| 归档时间: |
|
| 查看次数: |
1036 次 |
| 最近记录: |