Search code examples
regexbashforeachtclls

How can I search for a file using tcl, with only part of the file name


If I have a folder with the following files:

hello-version-1-090.txt
hello-awesome-well-091.txt
goodday-087.txt
hellooo-874.txt
hello_476.txt
hello_094.txt

How can I search for a file which has the term: 'hello' and '091' in it using tcl.

Possible solution: taking the output of an ls -l in a folder, splitting it with '\n' and then running a foreach on each line and using regexp to match the criteria. But how do I run an ls -l in a folder and record its save its contents (the file names) using tcl?


Solution

  • With glob, you can apply the pattern and can get the list of file names matching our criteria.

    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"
    } 
    

    The reason for using -nocomplain is to allow an empty list to be returned without error if there is no file matched with our search pattern.

    Output

    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                                                                                                     
    

    By the way, with respect to your query on how to save the ls -l output, simply save the output to a variable.

    set result [ exec ls -l ]
    

    Then with the result variable, you can apply regexp by means of looping line by line just as you have mentioned.

    But, I hope that using glob would be a better approach.

    Reference : glob