dollar sign literals

Discussion in 'Perl Misc' started by Jason Williams, Jul 3, 2007.

  1. 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
    Jason Williams, Jul 3, 2007
    #1
    1. Advertising

  2. Jason Williams wrote:
    > 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ürgen Exner, Jul 3, 2007
    #2
    1. Advertising

  3. Jason Williams wrote:
    > 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.


    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
    --
    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
    John W. Krahn, Jul 3, 2007
    #3
  4. On Tue, 03 Jul 2007 12:59:36 -0700, Jason Williams
    <> wrote:

    > 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.


    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
    --
    {$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
    (($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
    ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
    256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
    Michele Dondi, Jul 3, 2007
    #4
  5. Jason Williams

    Dave Weaver Guest

    Jason Williams <> wrote:
    >
    > 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
    Dave Weaver, Jul 4, 2007
    #5
  6. 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);
    Jason Williams, Jul 10, 2007
    #6
  7. Jason Williams wrote:
    > 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?

    > --------------------------------
    > #!/bin/ksh
    > PATH=blah
    > for FILE in dc0003*


    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";

    > done


    }

    jue
    Jürgen Exner, Jul 10, 2007
    #7
  8. Jürgen Exner wrote:
    > Jason Williams wrote:
    >> 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?
    >
    >> --------------------------------
    >> #!/bin/ksh
    >> PATH=blah
    >> for FILE in dc0003*

    >
    > for my $file (glob(dc0003*))


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

    >> do

    >
    > {
    >
    >> echo $FILE

    >
    > print $file;
    >
    >> mv $FILE $FILE.bak

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


    rename( $file, "$file.bak" ); #from perl

    >> 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);


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

    >> echo $FILE done

    >
    > print "$file done";
    >
    >> done

    >
    > }



    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
    John W. Krahn, Jul 10, 2007
    #8
  9. Jason Williams <> wrote:

    > #!/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> ) {


    --
    Tad McClellan
    email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
    Tad McClellan, Jul 11, 2007
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Norman Yuan
    Replies:
    0
    Views:
    850
    Norman Yuan
    Dec 8, 2005
  2. Marcus Leon
    Replies:
    1
    Views:
    2,995
    Matt Humphrey
    Jun 7, 2006
  3. Mark Hahn
    Replies:
    29
    Views:
    1,011
    Fredrik Lundh
    Apr 21, 2004
  4. belred

    u'\N{dollar sign}'

    belred, May 21, 2009, in forum: Python
    Replies:
    1
    Views:
    358
    John Machin
    May 21, 2009
  5. Dennis Walter

    Why use dollar sign $ for variables

    Dennis Walter, Jul 6, 2004, in forum: Perl Misc
    Replies:
    21
    Views:
    296
    Ian Wilson
    Jul 14, 2004
Loading...

Share This Page