Stop AWK from overwriting next section in file -
i have following awk script updates settings.py file.
original settings.py
databases = { 'default': { 'engine': 'django.db.backends.sqlite3', 'name': os.path.join(base_dir, 'db.sqlite3'), } } # password validation # https://docs.djangoproject.com/en/1.9/ref/settings/#auth-password-validators auth_password_validators = [ { 'name': 'django.contrib.auth.password_validation.userattributesimilarityvalidator', }, { 'name': 'django.contrib.auth.password_validation.minimumlengthvalidator', }, { 'name': 'django.contrib.auth.password_validation.commonpasswordvalidator', }, { 'name': 'django.contrib.auth.password_validation.numericpasswordvalidator', }, ]
here awk script updates database section works perfectly.
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" \047" db_name "\047,"; pr(sp,"user", db_user); pr(sp,"password", db_pass); pr(sp,"host","localhost"); pr(sp,"port",""); next }1'
output of new settings.py after awk script, updating auth section below.
databases = { 'default': { 'engine': 'django.db.backends.mysql', 'name': 'django', 'user': 'django', 'password': 'django', 'host': 'localhost', 'port': '', } } # password validation # https://docs.djangoproject.com/en/1.9/ref/settings/#auth-password-validators auth_password_validators = [ { 'name': 'django', 'user': 'django', 'password': 'django', 'host': 'localhost', 'port': '', },
how can stop awk overwriting auth section?
apply following update:
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"] } /databases/{ f=1 }/auth_password_validators/{ f=0 } /sqlite/{ sub(/sqlite[0-9]*/,"mysql",$0) } /name/ && f{ sp=substr($0,1,index($0,"\047")-1); print sp$1" \047" db_name "\047,"; pr(sp,"user", db_user); pr(sp,"password", db_pass); pr(sp,"host","localhost"); pr(sp,"port",""); next }1' settings.py
Comments
Post a Comment