Using globs in Perl replace one liner in TCL script -
i want run 1 perl 1 liner in tcl script below:
exec perl -i -pe {s/substring/replacing_string/g} testfile;
this works fine. but, if want modify files below:
exec perl -i -pe {s/substring/replacing_string/g} *;
it gives me error message:
can't open '*': no such file or directory. while executing exec perl -i -pe {s/substring/replacing_string/g} *;
i tried bracing '*', did not solve problem. requesting help...
assuming files a
, b
, , c
present in current working directory, executing echo *
in shell prints a b c
. because shell command evaluator recognizes wildcard characters , splices in list of 0 or more file names wildcard expression found.
tcl's command evaluator not recognize wildcard characters, passes them unsubstituted command invoked. if command can work wildcards so. exec
command doesn't, means pass wildcard expression shell command named command string.
testing this, get
% exec echo * *
because asked shell execute simply
echo *
if want wildcard expression expanded list of file names, need explicit call glob
command:
% exec echo [glob *] "a b c"
which still isn't quite right, since list wasn't automatically spliced command string: instead shell got
echo {a b c}
(note: i’m faking echo
on windows here, actual output might different.)
to both expand and splice list of file names, need this:
% exec echo {*}[glob *] b c
the {*}
prefix tells tcl command evaluator substitute following argument if words resulting arguments in original command line.
echo b c
this example, more concise explanation i've given here, in documentation exec
:
"if converting invocations involving shell globbing, should remember tcl not handle globbing or expand things multiple arguments default. instead should write things this:"
exec ls -l {*}[glob *.tcl]
ps:
if 1 has loaded fileutil
package:
package require fileutil
this can written one-liner in tcl too:
foreach file [glob *] {::fileutil::updateinplace $file {apply {str {regsub -all substring $str replacing_string}}}}
or line breaks , indentation readability:
foreach file [glob *] { ::fileutil::updateinplace $file { apply {str { regsub -all substring $str replacing_string }} } }
documentation: apply, exec, fileutil package, foreach, glob, package, regsub, {*}
Comments
Post a Comment