I call a grep/perl command to extract a substring from a file in a tcl script. However I can't find a way to escape $ in perl script.
Here is my command:
set varA [exec grep ^library $file | perl -pe 's/.*\(\s*"*(.*?)"*\s*\).*/$1/']
tcl will report error no variable '1'
. I escaped $
with {$}
per some guidance, however the perl command just doesn't work after the {}
escape. Any suggestion?
Wrap the entire argument in curly braces to prevent evaluation:
set varA [exec grep ^library $file | perl -pe {s/.*\(\s*"*(.*?)"*\s*\).*/$1/}]
Or skip the exec
and just do it all in tcl:
proc fetch_file {filename} {
set f [open $filename]
set result ""
while {[gets $f line] >= 0} {
if {[string equal -length 7 library $line]} {
lappend result [regsub {.*\(\s*"*(.*?)"*\s*\).*} $line {\1}]
}
}
close $f
return [join $result \n]
}
set varA [fetch_file $file]