I have the following Perl one liner code ,
My target is to replace the line after “=” separator, and that defined in PARAM variable
more file.txt PARAM=I WANT TO REPLACE THIS LINE bash-3.00# export VAL="THIS IS THE NEW LINE FOR EXAMPLE" bash-3.00# perl -i -pe 's/(PARAM=)+/${*}$ENV{VAL}/' file.txt bash-3.00# more file.txt THIS IS THE NEW LINE FOR EXAMPLEI WANT TO REPLACE THIS LINE
But actually its replace all the line include the PARAM itself
The expected results should be
more file.txt PARAM= THIS IS THE NEW LINE FOR EXAMPLE
Please advice what need to fix in my Perl one liner code?
other example that still not work:
bash-3.00# more file.txt PARAM=SOME STRING bash-3.00# export VAL="PUT THAT" bash-3.00# perl -i -pe 's/PARAM=K.+/$ENV{VAL}/' file.txt bash-3.00# more file.txt PARAM=SOME STRING
Advertisement
Answer
You can use K
(positive look behind) and .+
to remove everything after PARAM=
until newline,
perl -i -pe 's/PARAM=K.+/ $ENV{VAL}/' file.txt
or for older perl,
perl -i -pe 's/(?<=PARAM=).+/ $ENV{VAL}/' file.txt