-2

I have total 3 environment dev, stag and Prod all environment have config.json like this

{
  "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"
  }
}

Now I want to convert it into like this for all environment using shell script

dev environment config.json

{
  "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"
  }
}

How I can get this using sed or any other solution?

shas
  • 703
  • 2
  • 8
  • 31
soheshdoshi
  • 594
  • 3
  • 7
  • 24
  • Do you just want to add the prefix `dev_` to every value, or do you have a full set of development values you want to substitute for the existing values? – chepner Aug 22 '18 at 12:44
  • Honestly, the simplest thing to do is just maintain 3 separate files, and use the appropriate one at run time. – chepner Aug 22 '18 at 12:44
  • @chepner need for ci/cd environment for replace variables – soheshdoshi Aug 22 '18 at 12:48

1 Answers1

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

using sed u can do

Edit:

To insert pass -i

sed  -i 's/: "/: "dev_/g' config.json 
shas
  • 703
  • 2
  • 8
  • 31
  • 3
    `not working please check then reply` - possibly the most casually rude response to someone trying to help you I've every seen. How about "thanks" and a description of in what way it's "not working"? Imagine someone gave you a car for free and you turn around and just tell them "its not working, fix it then get back to me". – Ed Morton Aug 22 '18 at 14:47
  • Munging a structured format like JSON with regular expressions or simple replacements may or may not work for some cases, but in the long run I'd recommend getting a real parser that understands the syntax. – Robert Aug 22 '18 at 15:35