aldred
aldred

Reputation: 853

Using sed/bash to replace items in YAML file

I want to use bash script to replace the Addresses field in YAML file with the items in the variable ORDERER_LIST dynamically i.e. if there are 3 items in the ORDERER_LIST, the items in the Addresses field will also be 3. The order does not matter.

ORDERER_LIST="orderer0-orderer-org:7050 orderer1-orderer-org:7050"

Here is the sample yaml file:

Orderer: &OrdererDefaults
  OrdererType: kafka
  Addresses:
    - 1 (to be replaced with item in ORDERER_LIST)
    - 2 (to be replaced with item in ORDERER_LIST)
    - etc

Upvotes: 0

Views: 406

Answers (1)

mohit
mohit

Reputation: 2469

Rather than finding and replacing the elements. I think you should generate your YAML file with your bash script. You can do this in the below way.

#!/bin/bash
ORDERER_LIST="orderer0-orderer-org:7050 orderer1-orderer-org:7050"
IFS=' ' read -ra arr <<< "$ORDERER_LIST"
echo "Orderer: &OrdererDefaults
  OrdererType: kafka
  Addresses:"
for i in "${arr[@]}"
do
   echo "    - $i"
done

Output:

Orderer: &OrdererDefaults
  OrdererType: kafka
  Addresses:
    - orderer0-orderer-org:7050
    - orderer1-orderer-org:7050

Let me know if it helps.

Upvotes: 2

Related Questions