Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

problems with backreference

by cdlaforc (Novice)
on Sep 06, 2011 at 13:45 UTC ( #924385=perlquestion: print w/ replies, xml ) Need Help??
cdlaforc has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I'm trying to do the below substitution creating 4 back references and putting a 0 between them. I usually use ${1} syntax instead of \1, but I am running this command via ssh and this is not working. Because of this I don't know what to do between the \2 and 0 to make sure the second back reference is recognized. Any ideas? Thanks, Chris.
perl -pi -e 's/^(ISA\~.*?\~.*?\~.*?\~.*?\~.*?\~.*?\~.*?\~)(..........) +(....).(\~.*)$/\1\20\3\4/' PB385930.P0

Comment on problems with backreference
Download Code
Re: problems with backreference
by norbert.csongradi (Beadle) on Sep 06, 2011 at 14:21 UTC

    Please give some info about how you send it over SSH, there should be some problem there (in shell escapes I think)

    This should be better to send over SSH (not tested!):

    ssh user@host 'perl -pi -e "s/^(ISA~.*?~.*?~.*?~.*?~.*?~.*?~.*?~)(.... +......)(....).(~.*)\$/\\1\\20\\3\\4/"'

    Double escapes of \ chars become one backslash on the remote side, no need to escape tilde. Anyway, better to see your version still.

Re: problems with backreference
by ikegami (Pope) on Sep 06, 2011 at 15:33 UTC

    \1 is a regular expression pattern that matches what the first capture captured. It makes no sense to use it outside of regular expressions as you did.

    Perl script:

    s/^(ISA\~.*?\~.*?\~.*?\~.*?\~.*?\~.*?\~.*?\~)(..........)(....).(\~.*) +$/$1${2}0$3$4/

    Perl command:

    perl -i -pe's/^(ISA\~.*?\~.*?\~.*?\~.*?\~.*?\~.*?\~.*?\~)(..........)( +....).(\~.*)$/$1${2}0$3$4/' PB385930.P0

    ssh command:

    ssh ... 'perl -i -pe'\''s/^(ISA\~.*?\~.*?\~.*?\~.*?\~.*?\~.*?\~.*?\~)( +..........)(....).(\~.*)$/$1${2}0$3$4/'\'' PB385930.P0'

    Note the ' needed to be escaped when placed inside another ' literal.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (11)
As of 2015-06-03 12:10 GMT
Find Nodes?
    Voting Booth?

    What kind of chocolate gives you the most pleasure?

    Results (131 votes), past polls