Click here to get back home

separating substitutions from an embedded perl in a ksh script

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
separating substitutions from an embedded perl in a ksh script tazommers 04-04-2008
Get Chitika Premium
Posted by tazommers on April 4, 2008, 10:27 am
Please log in for more thread options
I have a large ksh script which had an embedded perl invocation to do
a character replace... to fix a date in a large flat file. I know the
script is inefficient, but I wanted to just improve the logic a bit
rather than have to install new scripts etc. Anyway, the file has
records that if start with the character "5" may need to have the date
replaced at position 70 in the file.

Here is the code snipet I have that works, except for one thing...

CMD="perl -p -i -n -e "'"s/^5(.)$EFF_DATE/5\$1|$REP_DATE/g"'"
$FILE"
eval $CMD

I don't want that "|" character, but I need something to separate the
$1 for the perl group and the $REP_DATE from the ksh. The $REP_DATE
gets replaced in ksh with the string 080407 for instance, so how does
one separate the $1 group from the string 080407?

Posted by David Harmon on April 4, 2008, 12:12 pm
Please log in for more thread options
On Fri, 4 Apr 2008 07:27:32 -0700 (PDT) in comp.lang.perl.misc,
tazommers@yahoo.com wrote,
>CMD="perl -p -i -n -e "'"s/^5(.)$EFF_DATE/5\$1|$REP_DATE/g"'"
>$FILE"
>eval $CMD
>
>I don't want that "|" character, but I need something to separate the
>$1 for the perl group and the $REP_DATE from the ksh. The $REP_DATE
>gets replaced in ksh with the string 080407 for instance, so how does
>one separate the $1 group from the string 080407?

Does writing $1 as do it?

But you are essentially replacing $1 with itself! That seems
gratuitous to me. The stuff before $EFF_DATE is just context and
should not participate in the replacement operation. Why not an
expression something more like:
s/(?<=^5.)$EFF_DATE/$REP_DATE/

The /g should not be necessary, you only want one replacement per
line anyway.

I can't figure you would need both the -p and -n switches.

Posted by tazommers on April 4, 2008, 12:34 pm
Please log in for more thread options
The {}'s did it. Thanks! Much headbanging over. :)

I tried the second suggestion and it didn't actually replace
anything... but doesn't that syntax remove the first 69 characters
from the actual string? Yes, I'm a perl rookie btw.

> Does writing $1 as do it?
>
> But you are essentially replacing $1 with itself! =A0That seems
> gratuitous to me. The stuff before $EFF_DATE is just context and
> should not participate in the replacement operation. =A0Why not an
> expression something more like:
> =A0 =A0 s/(?<=3D^5.)$EFF_DATE/$REP_DATE/
>
> The /g should not be necessary, you only want one replacement per
> line anyway.
>
> I can't figure you would need both the -p and -n switches.


Posted by John W. Krahn on April 4, 2008, 2:19 pm
Please log in for more thread options
David Harmon wrote:
> On Fri, 4 Apr 2008 07:27:32 -0700 (PDT) in comp.lang.perl.misc,
> tazommers@yahoo.com wrote,
>> CMD="perl -p -i -n -e "'"s/^5(.)$EFF_DATE/5\$1|$REP_DATE/g"'"
>> $FILE"
>> eval $CMD
>>
>> I don't want that "|" character, but I need something to separate the
>> $1 for the perl group and the $REP_DATE from the ksh. The $REP_DATE
>> gets replaced in ksh with the string 080407 for instance, so how does
>> one separate the $1 group from the string 080407?
>
> Does writing $1 as do it?
>
> But you are essentially replacing $1 with itself! That seems
> gratuitous to me. The stuff before $EFF_DATE is just context and
> should not participate in the replacement operation. Why not an
> expression something more like:
>
> s/(?<=^5.)$EFF_DATE/$REP_DATE/

Another way to do it:

/^5/ && substr( $_, 69 ) =~ s/^$EFF_DATE/$REP_DATE/


> The /g should not be necessary, you only want one replacement per
> line anyway.
>
> I can't figure you would need both the -p and -n switches.

From perlrun.pod under the '-p' entry: "A -p overrides a -n switch." so
the -n switch in the OP's example is superfuous.



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall

Posted by tazommers on April 4, 2008, 3:15 pm
Please log in for more thread options
> > I can't figure you would need both the -p and -n switches.
>
> =A0From perlrun.pod under the '-p' entry: "A -p overrides a -n switch." so=

> the -n switch in the OP's example is superfuous.

Heh, I didn't even look at what the switches meant... am editing
someone else's code and trying to improve on effeciency... it's a slow
script



Similar ThreadsPosted
Multiple Substitutions in perl October 16, 2005, 12:01 pm
Strange speed-increase by separating "if"s July 21, 2005, 11:51 am
separating attribution, quoted text, and sigs from the body of a post January 17, 2007, 6:36 am
Embedded Perl April 25, 2005, 9:44 am
"Incremental" Embedded Perl May 24, 2005, 8:49 am
Perl on embedded device June 10, 2008, 5:45 pm
Using strict and warnings in embedded perl July 14, 2004, 4:11 am
Using embedded PERL with commercial applications? November 23, 2004, 10:56 pm
Including modules in embedded perl with VC++ May 17, 2005, 11:56 pm
Error Handler in Embedded Perl May 23, 2005, 12:54 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap