Find and replace in many files

Find and replace in many files

am 23.10.2007 11:35:01 von Sailor

Hi,

I am working on several matlab m-files which were developed for old-
version matlab and can not run for the new-version. To make them work
for newer version, I have to replace the command " n==[]" to
"isempty(n)". However, the character(s) before "==[]" might be "err",
"w", "ind", or other characters. Is it possible to write a shell
script to find "==[]" in these files, recognize the character(s) in
front of ==[] , and then replace blabla==[] with isempty(blabla)?

There are must be a space before the character(s) and there is no
space between the character(s) and ==[]. All of thoese files have
extension .m.

Thanks in advance!

Find and replace in many files

am 23.10.2007 11:38:47 von Sailor

Hi,

I am working on several matlab m-files which were developed for
old-
version matlab and can not run for the new-version. To make them work
for newer version, I have to replace the command " n==[]" to
"isempty(n)". However, the character(s) before "==[]" might be "err",
"w", "ind", or other characters. Is it possible to write a shell
script to find "==[]" in these files, recognize the character(s) in
front of ==[] , and then replace blabla==[] with isempty(blabla)?

There must be a space before the character(s) and there is no
space between the character(s) and ==[]. All of those files have
extension .m.

Thanks in advance!

Re: Find and replace in many files

am 23.10.2007 11:49:14 von Cyrus Kriticos

Sailor wrote:
>
> I am working on several matlab m-files which were developed for
> old-
> version matlab and can not run for the new-version. To make them work
> for newer version, I have to replace the command " n==[]" to
> "isempty(n)". However, the character(s) before "==[]" might be "err",
> "w", "ind", or other characters. Is it possible to write a shell
> script to find "==[]" in these files, recognize the character(s) in
> front of ==[] , and then replace blabla==[] with isempty(blabla)?
>
> There must be a space before the character(s) and there is no
> space between the character(s) and ==[]. All of those files have
> extension .m.

sed -i 's/ \([a-zA-Z]*\)==\[\]/isempty(\1)/g' test.m

--
Best regards | Be nice to America or they'll bring democracy to
Cyrus | your country.

Re: Find and replace in many files

am 23.10.2007 11:50:03 von Cyrus Kriticos

Sailor wrote:
>
> I am working on several matlab m-files which were developed for
> old-
> version matlab and can not run for the new-version. To make them work
> for newer version, I have to replace the command " n==[]" to
> "isempty(n)". However, the character(s) before "==[]" might be "err",
> "w", "ind", or other characters. Is it possible to write a shell
> script to find "==[]" in these files, recognize the character(s) in
> front of ==[] , and then replace blabla==[] with isempty(blabla)?
>
> There must be a space before the character(s) and there is no
> space between the character(s) and ==[]. All of those files have
> extension .m.

sed -i 's/ \([a-zA-Z]*\)==\[\]/isempty(\1)/g' *.m

--
Best regards | Be nice to America or they'll bring democracy to
Cyrus | your country.

Re: Find and replace in many files

am 23.10.2007 13:42:39 von Ed Morton

On 10/23/2007 4:49 AM, Cyrus Kriticos wrote:
> Sailor wrote:
>
>> I am working on several matlab m-files which were developed for
>>old-
>> version matlab and can not run for the new-version. To make them work
>> for newer version, I have to replace the command " n==[]" to
>> "isempty(n)". However, the character(s) before "==[]" might be "err",
>> "w", "ind", or other characters. Is it possible to write a shell
>> script to find "==[]" in these files, recognize the character(s) in
>> front of ==[] , and then replace blabla==[] with isempty(blabla)?
>>
>> There must be a space before the character(s) and there is no
>> space between the character(s) and ==[]. All of those files have
>> extension .m.
>
>
> sed -i 's/ \([a-zA-Z]*\)==\[\]/isempty(\1)/g' test.m
>

The OP didn't say that the characters in the symbol before the == had to be
alphabetic, just that they couldn't be a space. Also, the above would delete the
space before that symbol and would match on there only being a space before the
==. Try this instead:

for file in *.m
do
sed 's/\([ \t]\)\([^ \t][^[ \t]*\)==\[\]/\1isempty(\2)/g' "$file" > tmp &&
mv tmp "$file"
done

I got rid of the -i as it's dangerous, not particularly useful, and not
available in all seds.

Regards,

Ed.

Re: Find and replace in many files

am 23.10.2007 22:25:55 von Michael Tosch

Ed Morton wrote:
>
> On 10/23/2007 4:49 AM, Cyrus Kriticos wrote:
>> Sailor wrote:
>>
>>> I am working on several matlab m-files which were developed for
>>> old-
>>> version matlab and can not run for the new-version. To make them work
>>> for newer version, I have to replace the command " n==[]" to
>>> "isempty(n)". However, the character(s) before "==[]" might be "err",
>>> "w", "ind", or other characters. Is it possible to write a shell
>>> script to find "==[]" in these files, recognize the character(s) in
>>> front of ==[] , and then replace blabla==[] with isempty(blabla)?
>>>
>>> There must be a space before the character(s) and there is no
>>> space between the character(s) and ==[]. All of those files have
>>> extension .m.
>>
>> sed -i 's/ \([a-zA-Z]*\)==\[\]/isempty(\1)/g' test.m
>>
>
> The OP didn't say that the characters in the symbol before the == had to be
> alphabetic, just that they couldn't be a space. Also, the above would delete the
> space before that symbol and would match on there only being a space before the
> ==. Try this instead:
>
> for file in *.m
> do
> sed 's/\([ \t]\)\([^ \t][^[ \t]*\)==\[\]/\1isempty(\2)/g' "$file" > tmp &&
> mv tmp "$file"
> done
>
> I got rid of the -i as it's dangerous, not particularly useful, and not
> available in all seds.
>
> Regards,
>
> Ed.
>

Dangerous yes, but the alternative has some pitfalls, too.
1.
Instead of

mv tmp "$file"

one should use

cp tmp "$file" &&
rm tmp

which retains the original inode/owner/modes.
2.
The simple name tmp could result in a name-matching conflict or a
security problem. Consider to use
"$tmp"
and define it like
tmp=/tmp/${0##*/}.$$
or, for most safety, use a private directory rather than the shared /tmp

--
Michael Tosch @ hp : com

Re: Find and replace in many files

am 26.10.2007 08:46:05 von Sailor

On 10 24 , 4 25 , Michael Tosch
wrote:
> Ed Morton wrote:
>
> > On 10/23/2007 4:49 AM, Cyrus Kriticos wrote:
> >> Sailor wrote:
>
> >>> I am working on several matlab m-files which were developed for
> >>> old-
> >>> version matlab and can not run for the new-version. To make them work
> >>> for newer version, I have to replace the command " n==[]" to
> >>> "isempty(n)". However, the character(s) before "==[]" might be "err",
> >>> "w", "ind", or other characters. Is it possible to write a shell
> >>> script to find "==[]" in these files, recognize the character(s) in
> >>> front of ==[] , and then replace blabla==[] with isempty(blabla)?
>
> >>> There must be a space before the character(s) and there is no
> >>> space between the character(s) and ==[]. All of those files have
> >>> extension .m.
>
> >> sed -i 's/ \([a-zA-Z]*\)==\[\]/isempty(\1)/g' test.m
>
> > The OP didn't say that the characters in the symbol before the == had to be
> > alphabetic, just that they couldn't be a space. Also, the above would delete the
> > space before that symbol and would match on there only being a space before the
> > ==. Try this instead:
>
> > for file in *.m
> > do
> > sed 's/\([ \t]\)\([^ \t][^[ \t]*\)==\[\]/\1isempty(\2)/g' "$file" > tmp &&
> > mv tmp "$file"
> > done
>
> > I got rid of the -i as it's dangerous, not particularly useful, and not
> > available in all seds.
>
> > Regards,
>
> > Ed.
>
> Dangerous yes, but the alternative has some pitfalls, too.
> 1.
> Instead of
>
> mv tmp "$file"
>
> one should use
>
> cp tmp "$file" &&
> rm tmp
>
> which retains the original inode/owner/modes.
> 2.
> The simple name tmp could result in a name-matching conflict or a
> security problem. Consider to use
> "$tmp"
> and define it like
> tmp=/tmp/${0##*/}.$$
> or, for most safety, use a private directory rather than the shared /tmp
>
> --
> Michael Tosch @ hp : com- -
>
> - -

Hi!

Thanks for all suggestions. Really appreciated!!!

Hsienwen