Re: can I replace a string in all Matlab .m files under a folder?
- From: Michael Wild <themiwi.REMOVE.THIS@xxxxxxxxxxxxxxx>
- Date: Sat, 09 Jun 2007 11:32:47 +0200
Walter Roberson wrote:
In article <f4b647$bvf$1@xxxxxxxxxxxxxxxxx>, Vista <abc@xxxxxxxx> wrote:
Does the Matlab Editor have the following feature:
Change string "XXXX" to "YYYY" in all the .m files under a folder, possilby recursively?
No.
Moreover,
How to write a program in Matlab to change the strings
"XXXX1" to "YYYY1"
"XXXX2" to "YYYY2"
In all .m files under a folder, based on a give list of XXXX and YYYY strings?
Can anybody show me how to write such a program to do this task?
Or, maybe the Perl in Matlab can help? Being not a Perl user, could anybody give me the example of exactly how to do this?
This is so much easier to do in Unix and perl that it is hardly worth
doing the Matlab.
In Unix,
find . -name '*.m' -exec perl -pi -f 's/XXXX/YYYY/g; s/ZZZZ/WWWW/g' '{}' ';'
The first part of this is the standard unix method of locating
all files from the current directory ('.') and in all subdirectories,
whose name is anything ending in .m . The -exec part means that for
each of them, the command that follows is to be executed. The '{}'
part near the end of the line is the signal to find to drop in the
current filename where the {} is, leading to a single-quoted filename
in that location on the command line; the quoting is important in case
the filename has spaces or other characters the shell would normally
treat specially. The ';' is a magic signal to find -exec that the
end of the embedded command has been reached.
So, for each .m file, perl -pi -f 's/XXXX/YYYY/g; s/ZZZZ/WWWW/g' 'FILENAME'
will be executed. The -i option signals that perl is to edit the
file "in place", by using a temporary output file that will be renamed
to the input file afterwards. The -p option signals that the perl commands
are to be executed for each line in the input file and that there
is an implicit "print" of the changed version of each line (only the
things that are printed will make it to the output file.) The -f
option signals that the following quoted string is the source code
to the perl program to be executed. The perl command
s/XXXX/YYYY/g means that for that line, one wishes to substitute (s)
strings that are originally XXXX and make them into YYYY, and that one
wishes to do that "globally" (g) for the entire line; that is, that
all XXXX on the line are to be made into YYYY. The semi-colon marks the
end of that command, and then there follows a command to
change all ZZZZ on the line to WWWW. Add more similar ones at need,
with semi-colon between each one.
Very similar programs can be used in Windows, but Windows quoting
is slightly different and possibly a minor change would have to be
made.
easier:
sed -i '.bak' 's/XXX/YYY/g' `find . -name '*.m'`
if you use some kind of bourne shell (bash, sh, ksh,...).
notice that the command part XXX is actually basic a regular expression. so pay attention what you put in there. if you want extended regexp, see man sed for instructions (on mac you'll have to add -E, on linux it probably is something else, no idea about cygwin or mingw).
michael
.
- References:
- can I replace a string in all Matlab .m files under a folder?
- From: Vista
- Re: can I replace a string in all Matlab .m files under a folder?
- From: Walter Roberson
- can I replace a string in all Matlab .m files under a folder?
- Prev by Date: Matlab with Finance
- Next by Date: Re: can I replace a string in all Matlab .m files under a folder?
- Previous by thread: Re: can I replace a string in all Matlab .m files under a folder?
- Next by thread: mcc problem
- Index(es):
Relevant Pages
|