I want to create a template of a config file containing credentials dynamically. The copy should keep the structure but with the values replaced by empty strings (i.e. ""). I use awk
for this purpose.
awk -v dq="\"" '{ if($NF ~ /^[&|\*|\n]/ || $1 == $NF) print $0; else {$NF=""; print $0 dq dq;} }' .config.yaml >> .config_temp.yaml
The problem is in the else statement. When I set $NF=""
the leading white space are not printed anymore. If I do not set the last field to an empty string, I do not observe this behavior but obviously do not receive the intended output (see below).
How can I set the last field as an empty string while keeping the leading white spaces?
I don't want to manually add a number of white spaces since the intent varies.
config.yaml (Input)
DEVELOPMENT: &development
<<: *common
check_access_token: False
database:
mongodb:
database: test
hostname: localhost
port: 27017
username: ""
password: ""
collection:
col_1: test_1
col_2: test_2
col_3: test_3
col_4: test_4
conf_temp.yaml (actual output)
DEVELOPMENT: &development
<<: *common
check_access_token: ""
database:
mongodb:
database: ""
hostname: ""
port: ""
username: ""
password: ""
collection:
property: ""
ctrl_voc: ""
form: ""
user: ""
Expected output
DEVELOPMENT: &development
<<: *common
check_access_token: ""
database:
mongodb:
database: ""
hostname: ""
port: ""
username: ""
password: ""
collection:
property: ""
ctrl_voc: ""
form: ""
user: ""
Edit (after Sundeep's replay)
Thank you for your answer. It almost works as I expect. However, I do not receive the same output as you. If I call
awk -F'[ ]' -v dq="\"" 'NF>1 && $NF !~ /^[*&]|:$/{$NF = dq dq} 1' .conf.yaml
I receive the following output:
DEVELOPMENT: &development
<<: *common
check_access_token: ""
"" <--
"" <--
database: ""
hostname: ""
port: ""
username: ""
password: ""
"" <--
property: ""
ctrl_voc: ""
form: ""
user: ""
The indention is as expected but the keys of the upper levels are replaced by quotes (see arrows).
I receive the same output, if I use your second suggestion with sed
.