atb atb - 1 year ago 45
Perl Question

Using a bash variable in regex replace

echo foo123 | perl -p -e 's/(\d+)/${replace}456/'

Is there a way to use a pre-defined variable in your replacement string? Maybe even a better alternative to

Answer Source
echo foo123 | perl -p -e 's/(\d+)/$ENV{replace}456/'

This grabs the shell variable from the environment in which Perl runs. In some circumstances you might need to export first:

export replace="bar"