Find the answer to your Linux question:
Results 1 to 3 of 3
I need to used some perl code in k shell. But it is giving me bad substitution. Same code works fine in bash. Any help would be appreciated. perl -i ...
  1. #1
    Just Joined!
    Join Date
    Jul 2009
    Posts
    1

    Need to used perl in k shell

    I need to used some perl code in k shell. But it is giving me bad substitution. Same code works fine in bash. Any help would be appreciated.

    perl -i -e '$re=q~'${1//~/\\~}'~; print "$re"; while (<>) { /$re/ ? $c++ : print;}exit($c ? 0 : 1)' /home/vmallya/testing || echo " Delete of lines containing."}
    ksh: "\$re=q~"${1//~/\\~}"~; print \"\$re\"; while (<>) { /\$re/ ? \$c++ : print;}exit(\$c ? 0 : 1)": bad substitution

  2. #2
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    Dare I ask why you have to use ksh? It's a scary world out there.

    In any event, I don't use it myself, but I checked out the ksh manual at:
    man/man1/ksh.html man page

    In the "Quoting" section, it discusses how even in single-quoted strings, the '$' will still cause variable interpolation. Therefore, I suggest replacing your single-quoted string with a double-quoted one and escaping everything as necessary. This will allow you to backslash-escape your $ and your internal double-quoted string.
    DISTRO=Arch
    Registered Linux User #388732

  3. #3
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi.

    The passage in the ksh online manual is:
    All characters enclosed between a pair of single quote marks ( ' ' ) that is not preceded by a $ are quoted. A single quote cannot appear within the single quotes. A single quoted string preceded by an unquoted $ is processed as an ANSI-C string except for the following ...

    -- excerpt from man/man1/ksh.html man page
    I read that as differentiating between something like:
    Code:
    'this is a line that uses \e as an escape'
    and
    Code:
    $'this is a line that uses \e as an escape'
    where the second line would cause an insertion of an ASCII ESCAPE for "\e"

    I ran this:
    Code:
    #!/usr/bin/env ksh
    
    # @(#) s1	Demonstrate quoting in ksh.
    
    echo
    set +o nounset
    LC_ALL=C ; LANG=C ; export LC_ALL LANG
    echo "Environment: LC_ALL = $LC_ALL, LANG = $LANG"
    echo "(Versions displayed with local utility \"version\")"
    version >/dev/null 2>&1 && version "=o" $(_eat $0 $1)
    set -o nounset
    echo
    
    FILE=${1-data1}
    cp data-orig $FILE
    
    echo " Data file $FILE:"
    cat $FILE
    
    echo
    echo " Results:"
    # perl -i -e '$re=q~'${1//~/\\~}'~; print "$re"; while (<>) { /$re/ ? $c++ : print;}exit($c ? 0 : 1)' /home/vmallya/testing || echo " Delete of lines containing."}
    perl -i -e '$re=q~'${1//~/\\~}'~; print "$re"; while (<>) { /$re/ ? $c++ : print;}exit($c ? 0 : 1)' $FILE || echo " Delete of lines containing."}
    
    exit 0
    (substituting my file for your data file) which produced:
    Code:
    % ./s1
    
    Environment: LC_ALL = C, LANG = C
    (Versions displayed with local utility "version")
    OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
    Distribution        : Debian GNU/Linux 5.0 
    ksh 93s+
    
     Data file data1:
    Now is the time
    for all good men
    to come to the aid
    of their country.
    
     Results:
    which produced no printable results, but no error message either. Running with bash shell produced the same (non-)results. The data file was empty after both runs.

    The trailing "}" on the echo seems unmatched.

    My recommendation would be to place the perl code in a separate file so that the quoting required by perl is really protected from the shell, and requires no mind-bending, alternate, additional quoting.

    Posting a very short data file and expected output also would be useful.

    Best wishes ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...