Find and replace in many files

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!
Sailor [ Di, 23 Oktober 2007 11:35 ] [ ID #1851865 ]

Find and replace in many files

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!
Sailor [ Di, 23 Oktober 2007 11:38 ] [ ID #1851866 ]

Re: Find and replace in many files

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.
Cyrus Kriticos [ Di, 23 Oktober 2007 11:49 ] [ ID #1851867 ]

Re: Find and replace in many files

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.
Cyrus Kriticos [ Di, 23 Oktober 2007 11:50 ] [ ID #1851868 ]

Re: Find and replace in many files

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.
Ed Morton [ Di, 23 Oktober 2007 13:42 ] [ ID #1851870 ]

Re: Find and replace in many files

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 [at] hp : com
Michael Tosch [ Di, 23 Oktober 2007 22:25 ] [ ID #1851898 ]

Re: Find and replace in many files

On 10 24 , 4 25 , Michael Tosch <eed... [at] NO.eed.SPAM.ericsson.PLS.se>
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 [at] hp : com- -
>
> - -

Hi!

Thanks for all suggestions. Really appreciated!!!

Hsienwen
Sailor [ Fr, 26 Oktober 2007 08:46 ] [ ID #1854882 ]
Linux » comp.unix.shell » Find and replace in many files

Vorheriges Thema: /usr/etc/dump disk-to-disk?
Nächstes Thema: find: incomplete statement