使用bash更改json文件中的变量

时间:2018-08-22 12:40:17

标签: bash shell sed

我总共有3个环境dev,stag和Prod,所有环境都具有 config.json 这样

    LOAD DATA LOCAL INFILE '$uploadfile' 
    REPLACE INTO TABLE mytable
    FIELDS TERMINATED BY ';' ENCLOSED BY '' 
    IGNORE 1 LINES  
    (`a`, `b`, `c`, `d`, `e`);

现在我想使用Shell脚本将其转换为适用于所有环境的

开发环境config.json

{
  "braintree": {
    "merchantid": "MERCHANTID",
    "publickey": "PUBLICKEY",
    "privatekey": "PRIVATEKEY"
  },
  "karix": {
    "url": "URL",
    "pass": "PASS",
    "user": "USER"
  },
  "fikarix": {
    "source": "SOURCE"

  },
  "mailgun": {
    "api_key": "API_KEY",
    "domain": "DOMAIN",
    "apikey": "APIKEY"
  },
  "paymentrails": {
    "key": "KEY",
    "environment": "ENVIRONMENT",
    "secret": "SECRET"
  }
}

如何使用{ "braintree": { "merchantid": "dev_MERCHANTID", "publickey": "dev_PUBLICKEY", "privatekey": "dev_PRIVATEKEY" }, "karix": { "url": "dev_URL", "pass": "dev_PASS", "user": "dev_USER" }, "fikarix": { "source": "dev_SOURCE" }, "mailgun": { "api_key": "dev_API_KEY", "domain": "dev_DOMAIN", "apikey": "dev_APIKEY" }, "paymentrails": { "key": "dev_KEY", "environment": "dev_ENVIRONMENT", "secret": "dev_SECRET" } } 或任何其他解决方案来获取此信息?

1 个答案:

答案 0 :(得分:1)

sed 's/: "/: "dev_/g' config.json

使用sed可以完成

修改:

要插入通行证-i

sed  -i 's/: "/: "dev_/g' config.json