Mihir
Mihir

Reputation: 557

perl regex with variables

set release=//packages/thirdparty/release/main.txt

perl -pe"s!(//packages/thirdparty/release/main.txt *)#\d+!$1#15!;" testlog.txt

How can I use %release% (environment variable release) instead of //packages/thirdparty/release/main.txt?

I'm getting syntax error from

 perl -pe"s!(%release%! *)#\d+!$1#15!;" testlog.txt

original question Search and replace a string using Windows shell or Perl script

Upvotes: 1

Views: 896

Answers (1)

Borodin
Borodin

Reputation: 126722

You can use the %ENV hash to access environment variables, like this

perl -pe"s/(\Q$ENV{RELEASE}\E *)#\d+/$1#15/" testlog.txt

You can also use a positive look-behind to avoid having to replace the initial string

perl -pe"s/(?<=\Q$ENV{RELEASE}\E *)#\d+/#15/" testlog.txt

or \K if you are running version 14 or later of Perl 5

perl -pe"s/\Q$ENV{RELEASE}\E *\K#\d+/#15/" testlog.txt

Upvotes: 3

Related Questions