Reputation: 664
I am currently working with editing/modifying yml files. I am a bit new using yq and unsure if I am going overboard using the tool. I am trying to replace two values in the yaml file with two environment values(linux system) as shown below. I am getting an invalid key reference error just simply trying to read the fields of that field using yq. What will be the best approach for modifying the fields the yml file? Would it be better to use sed or awk?
Environment Variables
$APP_KEY="DSDedfwerer02"
$APP_NAME="Test1"
test.yml
common: &default_settings
app_key: ""
app_name: ""
some_key1: "test"
some_key2: "onetwo"
some_key3: "filename"
another_key_n: "somevalue"
Desired result: test.yml
common: &default_settings
app_key: "DSDedfwerer02"
app_name: "Test1"
Upvotes: 1
Views: 1141
Reputation: 85590
With mikefarah/yq, it is relatively straightforward to just import the environment variables to the local context and update the required YAML elements
k="$APP_KEY" n="$APP_NAME" yq '.common |= ( . + {"app_key": strenv(k), "app_name": strenv(n)} ) | ..style="double"' yaml
Use the -i
or --inplace
flag to update the file inplace. Tested on version v4.25.3
Upvotes: 2
Reputation: 203502
In case you don't get a good yq
answer, here's how to do what you're asking for in any awk given the sample input you provided:
$ APP_KEY="DSDedfwerer02"
$ APP_NAME="Test1"
$ awk -v key="$APP_KEY" -v name="$APP_NAME" '
BEGIN { map["app_key:"]=key; map["app_name:"]=name }
$1 in map { sub(/".*/,""); $0=$0 "\"" map[$1] "\"" }
1' file
common: &default_settings
app_key: "DSDedfwerer02"
app_name: "Test1"
That will work whether your shell variables really are environment variables or not and assuming they don't contain any backslashes and assuming app_key:
and app_name:
only appear as the first strings in the input in the positions you want their values replaced.
If they truly are environment variables then you can do:
awk '
BEGIN { map["app_key:"]=ENVIRON["APP_KEY"]; map["app_name:"]=ENVIRON["APP_NAME"] }
$1 in map { sub(/".*/,""); $0=$0 "\"" map[$1] "\"" }
1' file
or if not:
APP_KEY="$APP_KEY" APP_NAME="$APP_NAME" \
awk '
BEGIN { map["app_key:"]=ENVIRON["APP_KEY"]; map["app_name:"]=ENVIRON["APP_NAME"] }
$1 in map { sub(/".*/,""); $0=$0 "\"" map[$1] "\"" }
1' file
which will work even if they contain backslashes.
Upvotes: 1