AWK passing ENV variables -
i have following code pass env variables awk.
awk 'function pr(sp, k, v){ # prints key-value pair indentation printf "%s\047%s\047: \047%s\047,\n",sp,k,v; } begin { db_user = environ["db_user"] db_pass = environ["db_pass"] db_name = environ["db_name"] } /sqlite/{ sub(/sqlite[0-9]*/,"mysql",$0) } /name/{ sp=substr($0,1,index($0,"\047")-1); print sp$1" \047db_name\047,"; pr(sp,"user", db_user); pr(sp,"password", db_pass); pr(sp,"host","localhost"); pr(sp,"port",""); next }1'
i can db_name replaced env variable.
db_name = django
my results are:
databases = { 'default': { 'engine': 'django.db.backends.mysql', 'name': 'db_name', 'user': 'django', 'password': 'django', 'host': 'localhost', 'port': '', } }
the problem incorrect use of print
statement in awk
. seen gnu awk print()
page, need move variable outside quotes see expanded value.
print sp$1" \047db_name\047,"; # ^^^^^^^ awk not understand db_name within quotes
just make outside quotes below.
print sp$1" \047"db_name"\047,"
(or) use printf
altogether separate format specifiers variables , below
printf "%s %s \047%s\047,\n",sp,$1,db_name
Comments
Post a Comment