dollar sign literals

J

Jason Williams

Hi all, somewhat new to perl and unix scripting, here.

I am trying to run a unix ksh script to exec a perl command on each
file in a dir. Problem is, I need to include a dollar sign literal in
the right side of a substitution, like so:
-------------------------------
#!/bin/ksh
PATH=$PATH:/dbmgtu01/app/oracle/orbitz/1.0.0/bin:.
for FILE in dc0003*
do
echo $FILE
cp $FILE $FILE.bk
perl -pi -e "s/(.*)dc0003\.maintain\.partitions(.*dev.*)/
\1$calling_program_dev\2/g" $FILE
done
------------------------------

Only this does not work, because perl wants to replace the
$calling_program_dev with a ksh variable.

Here's some sample input and output:
input:
$calling_program_dir/dc0003.maintain.partitions_test -i dev_do9d

actual output:
$calling_program_dir/ -i dev_do9d

_desired output_ :
$calling_program_dir/$calling_program_dev -i dev_do9d

Any help appreciated. I've tried all sorts of things, including to
escape the dollar sign, enclose it in single quotes, etc, to no avail.

Many thanks,
Jason
 
J

Jürgen Exner

Jason said:
Hi all, somewhat new to perl and unix scripting, here. [...]
perl -pi -e "s/(.*)dc0003\.maintain\.partitions(.*dev.*)/
\1$calling_program_dev\2/g" $FILE

Only this does not work, because perl wants to replace the
$calling_program_dev with a ksh variable.

Actually no. Perl doesn't know and doesn't care about ksh variables. It is
the ksh shell that does this expansion, the $calling_programm_dev never
reaches the perl interpreter.

So you will need to check the documentation of ksh how to prevent expansion
of variables in ksh.

As far as Perl is concerned the
s/(.*)dc0003\.maintain\.partitions(.*dev.*)/\1$calling_program_dev\2/g
is Perl code, so perl will evaluate it and expand any _Perl_ variable it
will find, too.
So you want a literal dollar sign, then you need to ensure, that perl will
see a \$calling_program_dev.

jue
 
J

John W. Krahn

Jason said:
Hi all, somewhat new to perl and unix scripting, here.

I am trying to run a unix ksh script to exec a perl command on each
file in a dir. Problem is, I need to include a dollar sign literal in
the right side of a substitution, like so:
-------------------------------
#!/bin/ksh
PATH=$PATH:/dbmgtu01/app/oracle/orbitz/1.0.0/bin:.
for FILE in dc0003*
do
echo $FILE
cp $FILE $FILE.bk
perl -pi -e "s/(.*)dc0003\.maintain\.partitions(.*dev.*)/
\1$calling_program_dev\2/g" $FILE
done

Did you try it with single quotes instead of double quotes? Also the use of
\1 and \2 should only be used inside a regular expression.


perl -pi -e
's/(.*)dc0003\.maintain\.partitions(.*dev.*)/$1$calling_program_dev$2/g' $FILE



John
 
M

Michele Dondi

perl -pi -e "s/(.*)dc0003\.maintain\.partitions(.*dev.*)/
\1$calling_program_dev\2/g" $FILE
done

No, it's ksh than wants to replace the $calling_program_dev with its
own $calling_program_dev variable. Just use ksh's quoting mechanism to
prevent this, which I guess probably can amount to just
\$calling_program_dev.


Michele
 
D

Dave Weaver

Jason Williams said:
I am trying to run a unix ksh script to exec a perl command on each
file in a dir. Problem is, I need to include a dollar sign literal in
the right side of a substitution, like so:
-------------------------------
#!/bin/ksh
PATH=$PATH:/dbmgtu01/app/oracle/orbitz/1.0.0/bin:.
for FILE in dc0003*
do
echo $FILE
cp $FILE $FILE.bk
perl -pi -e "s/(.*)dc0003\.maintain\.partitions(.*dev.*)/
\1$calling_program_dev\2/g" $FILE
done
------------------------------

As others have said, this is really a ksh quoting problem.
You want perl to see .../\1\$calling_program_dev\2/...

First, enclose the Perl code in single quotes, to ensure perl sees the
$, then you'll need to escape it so that it's not interpreted as a Perl
variable.

So:

perl -pi -e 's/(.*)dc0003\.maintain\.partitions(.*dev.*)/
\1\$calling_program_dev\2/g' $FILE

More on-topic, the initial (.*), the corresponding \1, adn the trailing .* in
your regex are redundant, and it's better to use $1 instead of \1 in the
replacement part of the substitution:

perl -pi -e 's/dc0003\.maintain\.partitions(.*dev)/
\$calling_program_dev$1/g' $FILE
 
J

Jason Williams

Thank you all for you posts; I learned something from each of them. I
ended up taking a slightly different approach. I wrote a separate perl
program, and then called it from ksh for each file, like so:

ksh:
--------------------------------
#!/bin/ksh
PATH=blah
for FILE in dc0003*
do
echo $FILE
mv $FILE $FILE.bak
update_part_maint_jobs.pl $FILE.bak
mv $FILE.bak.new $FILE
echo $FILE done
done

perl:
------------------------------
#!/usr/bin/perl
open(infile, "$ARGV[0]");
open(outfile, ">$ARGV[0].new");
while (<infile>) {
s/.*calling_program_dir=.*//;
s/dc0003\.maintain\.partitions_test/dc0003.maintain.partitions/;
s/(.*)dc0003\.maintain\.partitions(.*dev.*)/$1\$calling_program_dev
$2/;
print outfile $_;
}
close(outfile);
 
J

Jürgen Exner

Jason said:
Thank you all for you posts; I learned something from each of them. I
ended up taking a slightly different approach. I wrote a separate perl
program, and then called it from ksh for each file, like so:

ksh:

Any particular reason why you are not using Perl for this part?

for my $file (glob(dc0003*))
do
{

echo $FILE

print $file;
mv $FILE $FILE.bak

move ($file, "$file.bak"); #from File::Copy
update_part_maint_jobs.pl $FILE.bak

#rewritten to call a sub:
update_part_jobs ("$file.bak");
mv $FILE.bak.new $FILE

move ("$file.bak.new", $file);
echo $FILE done

print "$file done";

}

jue
 
J

John W. Krahn

Jürgen Exner said:
Any particular reason why you are not using Perl for this part?

for my $file (glob(dc0003*))

for my $file (glob("dc0003*"))
print $file;


move ($file, "$file.bak"); #from File::Copy

rename( $file, "$file.bak" ); #from perl
#rewritten to call a sub:
update_part_jobs ("$file.bak");


move ("$file.bak.new", $file);

rename( "$file.bak.new", $file );
print "$file done";


}


John
 
T

Tad McClellan

Jason Williams said:
#!/usr/bin/perl

use strict;
use warnings;

open(infile, "$ARGV[0]");


You should use UPPER CASE for filehandles.

You should always, yes *always*, check the return value from open.

You should not use useless quotes.

perldoc -q vars

What’s wrong with always quoting "$vars"?

open(INFILE, $ARGV[0]) or die "could not open '$ARGV[0]' $!";

Better yet, you should use the 3-argument form of open:

open(my $infile, '<', $ARGV[0]) or die "could not open '$ARGV[0]' $!";

while (<infile>) {

while ( <$infile> ) {
 

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,780
Messages
2,569,607
Members
45,240
Latest member
pashute

Latest Threads

Top