Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

$` $' in bash??

by ZWcarp (Beadle)
on Jul 20, 2011 at 17:13 UTC ( #915685=perlquestion: print w/ replies, xml ) Need Help??
ZWcarp has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks, I was wondering if there is a way to use $` and $' in bash commands... lets say

perl -lane '$F[1]=~s/(chr\w[^_]?)_.*\b/$1/;print $`. "\t". $F[1] ."\t" .$'' (which does not work because the apostrophe ends the executable... \' doesn't work either) .

Also why is it that \1 doesn't work... is using $1 the correct way to do this? If anyone knows of a reference that documents what special characters translate to bash commands it would be very appreciated. Thank you for your help.

Comment on $` $' in bash??
Download Code
Re: $` $' in bash??
by Sandy (Curate) on Jul 20, 2011 at 17:22 UTC
    use English
    perl -lane 'use English;$F[1]=~s/(chr\w[^_]?)_.*\b/$1/;print $PREMATC +H. "\t". $F[1] ."\t" .$POSTMATCH'

      It's silly to keep using the bad variables instead of /p if you're going to use the English names.

      perl -lane '$F[1]=~s/(chr\w[^_]?)_.*\b/$1/p;print ${^PREMATCH}. "\t". +$F[1] ."\t" .${^POSTMATCH}'
        Actually, I never use English, so I am not sure what it is that you are saying.

        bad variables??

        What is the reason for using ${^PREMATCH} instead of $PREMATCH?

Re: $` $' in bash??
by ikegami (Pope) on Jul 20, 2011 at 18:01 UTC

    There's no issue with «`» (within single quotes). The problem is with «'».

    Ways of making «foo$'bar$`baz» into a bash literal:

    foo\$\'bar\$\`baz "foo\$'bar\$`baz" 'foo$'"'"'bar$`baz' 'foo$'\''bar$`baz'

    Of course, you could use avoid using «'», starting by writing code that's much clearer.

    perl -lanE'$F[1]=~/(.*?)(chr\w[^_]?)_.*\b(.*)/; say "$1\t$1$2$3\t$3"'

    The pattern looks very fishy. /.*\b.*/? There are clearer and safer ways of writing that. I have no idea what you are trying to match with that.

Re: $` $' in bash??
by JavaFan (Canon) on Jul 20, 2011 at 20:16 UTC
    Using a standard trick to include a single quote:
    perl -lane '$F[1]=~s/(chr\w[^_]?)_.*\b/$1/;print $`. "\t". $F[1] ."\t" + .$'"'"
    The single quote directly following the $ matches the first single quote. In Perl, this will end a string, but not so in the shell - here single quotes act like \Q/\E in Perl. Directly following the $ and the single quote is "'", which for the shell is just a single quote. Which makes that the Perl program ends in $', as intended.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://915685]
Approved by Sandy
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (13)
As of 2015-07-03 12:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (53 votes), past polls