Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String match question

Status
Not open for further replies.

fabien

Technical User
Sep 25, 2001
299
AU
Hi!

I would like to test some files in a directory and extract all those that have _README in the end

I did something like
foreach $f [lsort -dictionary [glob file join $dirname *]]]
{
puts $f -> gives something like /dsdsd/dsdsd/sdsd/hhj_README...

set a [regexp {*(/*README) } $f] -> I want to test _README at the end of each $f

if {$a==1}
{
puts "match"
}

}

I have tried string match as well without luck. Can someone please help me find the right syntax.

Thanks,

Fabien
 
If simply do
regexp (_README} $f I get all the _README files but I also get those like _README.txt etc, what should I add to restrict the test?

In addition how can I "trim" the _README from the file name?

Thanks,

Fabien
 
Far more straightforward is to use a match pattern in your glob command that retrieves only the "README" files:

[tt]glob -nocomplain [ignore][[/ignore]file join $dirname *_README[ignore]][/ignore][/tt]

Also notice the use of the -nocomplain option. Without it, glob raises an error condition if it doesn't find any files that match the pattern. (Stupid, I agree.) The -nocomplain option causes it simply to return an empty list rather than raising the error.

As for trimming the "_README" from the end, you've got a few options. The simplest, as it's a fixed string pattern, is to use string range to return all but the last 7 characters of the string:

[tt]% set file "Unix_README"
Unix_README
% set newfile [ignore][string range $file 0 end-7][/ignore]
Unix[/tt] - Ken Jones, President
Avia Training and Consulting
866-TCL-HELP (866-825-4357) US Toll free
415-643-8692 Voice
415-643-8697 Fax
 
Another way that is fairly straightforward:

proc all_matches {cnt {dir ""} {L1 {}}} {
if {$dir != ""} {
foreach name [glob $dir*] {
if {[regexp "(.*)\._README" $name all m1]} {
lappend L1 [list "[incr cnt]:[file join $name]"]
}
}
} else {
error "Must have directory to search."
return 1
}
catch {puts [lsort $L1]}
return
}

The additional pat matching from regexp can help
with nitpicky details, and tshooting.

To use:
windows
all_matches 0 c:/
*nix
all_matches 0 /home/me/
 
Thanks a lot, this was very helpful!

Fabien
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top