I'm very new to awk, I'm trying to do the following in an environment that doesn't support grep -P
and doesn't have Perl installed either.
Given I have a list of env variables starting with a prefix, I want to generate an argument list based on those variables, without using the prefix.
So, for example, if I have
PREFIX_VARIABLE_1=100
PREFIX_VARIABLE_2=500
I'd like to generate
--set service.VARIABLE_1=100 --set service.VARIABLE_2=500
I've came up with
env | awk 'match($0, /PREFIX_.*/) { substr($0, RSTART+8, RLENGTH) ; split($0, parts, "=") ; printf "--set service.%s=%s ", parts[1], parts[2] }'
but this doesn't strip the prefix, substr
is not being used I guess
With grep -P
I can do it:
env | grep -oP '(?<=PREFIX_)(.*)' | awk 'split($0, parts, "=") { printf "--set service.%s=%s ", parts[1], parts[2] }'
Any idea how to chain/pipe multiple awk functions properly? Thanks
gawk
using option--lint
is a good idea while developing, BTW.