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.

7
  • Not clear, could you please mention more details? Like which file you want to edit? from where variable values should be picked up? Commented Aug 18, 2018 at 6:12
  • how is python related in this question? seems like javascript to me Commented Aug 18, 2018 at 6:13
  • @RavinderSingh13 file already given and variable value should be anything Commented Aug 18, 2018 at 6:20
  • @soheshdoshi if you know what you are replacing exactly then why not just do a sed "s/$APIKEY/$Master\_$APIKEY" Commented Aug 18, 2018 at 6:57
  • This uses sed, which has nothing to do with bash. Do you require bash code to do this? Commented Aug 18, 2018 at 7:06

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

what about FUNCTIONSURL?
Wif you want to replace FUNCTIONSURL too then replace \(.*: "\) with \(.*: *"\) in the regex in order to match zero or more spaces after the colon.
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",

};

2 Comments

It's give output like apikey : "REPLACEMENT_xyzv" time for replacing placeholder with environment variable
@sheshdoshi can u please share more details about the variable and your implementation??

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.