0
export const environment = {
  production: true,
  firebase: {
    apiKey: "APIKEY",
    authDomain: "AUTHDOMAIN",
    databaseURL: "DATABASEURL",
    projectId: "PROJECTID",
    storageBucket: "STORAGEBUCKET",
    messagingSenderId: "MESSAGINGSENDERID"
  },
  functionURL:"FUNCTIONSURL",

};

I have this type of file now I want to replace all variables (APIKEY, AUTHDOMAIN,..) using bash, please provide some generic solution!

sed  -i 's/($APIKEY)/('"$Master_APIKEY"')/g'

Already use for every variable in file.

leiyc
  • 903
  • 11
  • 23
soheshdoshi
  • 594
  • 3
  • 7
  • 24
  • Not clear, could you please mention more details? Like which file you want to edit? from where variable values should be picked up? – RavinderSingh13 Aug 18 '18 at 06:12
  • how is python related in this question? seems like javascript to me – Arghya Saha Aug 18 '18 at 06:13
  • @RavinderSingh13 file already given and variable value should be anything – soheshdoshi Aug 18 '18 at 06:20
  • @soheshdoshi if you know what you are replacing exactly then why not just do a `sed "s/$APIKEY/$Master\_$APIKEY"` – Inder Aug 18 '18 at 06:57
  • This uses `sed`, which has nothing to do with `bash`. Do you require `bash` code to do this? – cdarke Aug 18 '18 at 07:06
  • @soheshdoshi: See: [Difference between single and double quotes in bash](http://stackoverflow.com/q/6697753/3776858) – Cyrus Aug 18 '18 at 09:23
  • Do you have a variabele called `APIKEY` with the key to be replaced (like `apiKey`), and the new value that should replace the old one in the variable `Master_APIKEY`? Perhaps start writing your shell varables in lowercase now. – Walter A Aug 18 '18 at 22:27

2 Answers2

2

To prepend every variable with the string $Master_ use:

sed 's/\(.*: "\)\(.*\)"/\1$Master_\2"/g' inputfile

this command works like this:

  • \(.*: "\): match everything until the string : " and capture in group 1

  • \(.*\)": match everything before the last " and capture in group 2

  • \1$Master_\2": replace with contents of group 1, followed by $Master_ and contents of group 2.

output:

export const environment = {                                                
  production: true,                                                         
  firebase: {                                                               
    apiKey: "$Master_APIKEY",                                               
    authDomain: "$Master_AUTHDOMAIN",                                       
    databaseURL: "$Master_DATABASEURL",                                     
    projectId: "$Master_PROJECTID",                                         
    storageBucket: "$Master_STORAGEBUCKET",                                 
    messagingSenderId: "$Master_MESSAGINGSENDERID"                          
  },                                                                        
  functionURL:"FUNCTIONSURL",                                               

};

you can include the -i.bak flag to replace the original file and make a backup file.

builder-7000
  • 7,131
  • 3
  • 19
  • 43
2

Here is an example for what you are trying to acheive:

P.S. you don't need to cat and all, you can directly use sed -i on the file, this is just to demonstrate a single line code where we are inserting a variable on the given pattern:

a=REPLACEMENT ;cat inputfile | sed "s/\"\(.*\)\"/\""$a"_\1\"/g"

the output from the input file provided by you :

export const environment = {
  production: true,
  firebase: {
    apiKey: "REPLACEMENT_APIKEY",
    authDomain: "REPLACEMENT_AUTHDOMAIN",
    databaseURL: "REPLACEMENT_DATABASEURL",
    projectId: "REPLACEMENT_PROJECTID",
    storageBucket: "REPLACEMENT_STORAGEBUCKET",
    messagingSenderId: "REPLACEMENT_MESSAGINGSENDERID"
  },
  functionURL:"REPLACEMENT_FUNCTIONSURL",

};
tamara
  • 27
  • 5
Inder
  • 3,711
  • 9
  • 27
  • 42