randy1234 has asked for the wisdom of the Perl Monks concerning the following question:

I'm having trouble using back-references in a Windows one-liner and hoping someone knows what I'm doing wrong.

I can use a single back-reference but as soon as I try more than one it breaks.

Environment:

> echo "abcd" | perl -nle '/(b).(d)/ && print "$1" ' b > echo "abcd" | perl -nle '/(b).(d)/ && print "$2" ' d > echo "abcd" | perl -nle '/(b).(d)/ && print "$1 $2" ' Can't open $2 : No such file or directory.

The error changed when I tried to escape the double-quotes with a backslash and a back-tick:

> echo "abcd" | perl -nle '/(b).(d)/ && print \`"$1 $2\`" ' Can't find string terminator "`" anywhere before EOF at -e line 1.

Any ideas?

Replies are listed 'Best First'.
Re: Trouble with back-references on Windows
by poj (Abbot) on Mar 14, 2019 at 19:01 UTC
    Any ideas?
    echo "abcd" | perl -nle '/(b).(d)/ && print \"$1 $2\"'
    
    poj

      Thanks poj!

      I was using quotes for a more complicated regex earlier in the day (without the back-references) and found that it only worked with BOTH the backslash and the back-tick so I neglected to try it it in this instance.

Re: Trouble with back-references on Windows
by Laurent_R (Canon) on Mar 14, 2019 at 23:23 UTC
    Quoting is somewhat of a mess in Perl one-liners under Windows (which is one of the reasons why I prefer to use Cygwin or bash for Windows when I have to work with Windows). It is usually better to use double quotes for the one-liner script, but that makes interpolating quotes slightly more difficult to use.

    This works with the Windows command prompt:

    C:\Users\Laurent>echo "abcd" | perl -nle "/(b).(d)/ && print qq/$1 $2/ +;" b d