I have a variable data="password: password@123, url: url@123, username: user@123"
.
I am trying to split this using the delimiter comma(,
). I came up with the following script and it works well in CentOS Linux. But the same does not work in Debian GNU/Linux 9 (Stretch).
IFS=, read -a array <<< $data
echo ${array[1]}
echo ${array[2]}
echo ${array[3]}
Gives the output (CentOS Linux):
password: password@123
url: url@123
username: user@123
But for Debian GNU/Linux 9 (Stretch) the output is empty. Kindly help me with it!
EDIT: The value for data is obtained from a JSON response.
data=$(vault kv get -format=json myapp/database | jq '.data' | sed '0,/{/ s/{//' | sed -r 's/(.*)}/\1 /' | sed s/\"//g)
echo $data
password: password@123, url: url@123, username: user@123
The response of vault kv get -format=json myapp/database
:
{
"request_id": "5eee87f9-16dc-fd62-e249-8737a6cd7041",
"lease_id": "",
"lease_duration": 2764800,
"renewable": false,
"data": {
"password": "password@123",
"url": "url@123",
"username": "user@123"
},
"warnings": null
}
SOLUTION: This script works in both Centos and Ubuntu
data=$(vault kv get -format=json myapp/database | jq '.data')
data=$(echo $data | sed 's/ //g' | sed '0,/{/ s/{//' | sed -r 's/(.*)}/\1 /' | sed 's/\"//g')
IFS=, read -a array <<< $data