Find and Replace Howto

A

Anuradha

Hi,

I am newbie to Perl and would like to know if this possible doing in
Perl.

I have huge number of .4gl files. I need to find and replace the
string which contains mySchema.TO_CHAR( to CHAR( .

This can be accomplished in one-liner. While the problem I am facing
is
mySchema.TO_CHAR() is overloaded with arguments. I need to replace
ones which does not have ',' to TO_CHAR(). Here also care should be
taken that
TO_CHAR ( SUBSTR()) can also be done. so while searching it should
handle this also.

Any idea if this can be done in Perl or better do it manually.

Let me know and thanks for reading this query.

Anu
 
G

Gunnar Hjalmarsson

Anuradha said:
I have huge number of .4gl files. I need to find and replace the
string which contains mySchema.TO_CHAR( to CHAR( .

This can be accomplished in one-liner. While the problem I am
facing is mySchema.TO_CHAR() is overloaded with arguments. I need
to replace ones which does not have ',' to TO_CHAR(). Here also
care should be taken that TO_CHAR ( SUBSTR()) can also be done. so
while searching it should handle this also.

Any idea if this can be done in Perl or better do it manually.

I'm convinced it can be done in Perl, and if the number of files is
"huge", it is probably advisable.

http://www.perldoc.com/perl5.8.0/pod/perlintro.html#Files-and-I-O

http://www.perldoc.com/perl5.8.0/pod/perlre.html
 
S

Simon Taylor

Anuradha said:
I am newbie to Perl and would like to know if this possible doing in
Perl.

I have huge number of .4gl files. I need to find and replace the
string which contains mySchema.TO_CHAR( to CHAR( .

This can be accomplished in one-liner. While the problem I am facing
is
mySchema.TO_CHAR() is overloaded with arguments. I need to replace
ones which does not have ',' to TO_CHAR(). Here also care should be
taken that
TO_CHAR ( SUBSTR()) can also be done. so while searching it should
handle this also.

Any idea if this can be done in Perl or better do it manually.

Let me know and thanks for reading this query.

Anu

Hello Anu,

When I've dealt with transformations like this I first consider whether
or not the data that needs to be transformed will always
appear in some regular (and trivial) pattern, or whether it may indeed
appear in any pattern that is legally acceptable to the program that was
designed to read it.

What I mean by this is if you have lines that are all of the format:

mySchema.TO_CHAR(12)
mySchema.TO_CHAR(12, 2)
mySchema.TO_CHAR(substr(blah, blah), 10, 2)

then regular expressions may be the best solution


However, if the data is more variable:

mySchema.TO_CHAR(
12
)
other(things) that are
unrelated('mySchema').....
mySchema.TO_CHAR( -- oops a legal SQL comment?
12);
mySchema.TO_CHAR
(12, 2)
mySchema.TO_CHAR(
substr(blah
, blah), 10, 2)

then it may pay you to build a little parser to process the data.

It can be difficult working out how to strike a good balance between
these two approaches.

However in my experience doing similar transformations, the input is
usually rather orderly and lends itself well to transformation via
simple regular expressions.

Why don't you follow up this post with a small sample of the existing
data, *exactly* as it appears in the ".4gl" file, and how this data
should be transformed.

Hope this helps,

Simon Taylor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top