Home > Software design >  How to use sed to migrate from process.env.MY_VAR to env.get('MY_VAR').required() using se
How to use sed to migrate from process.env.MY_VAR to env.get('MY_VAR').required() using se

Time:04-28

I'd like to migrate from dotenv to env-var npm package for a dozen of repositories.

Therefore I am looking for a smart and easy way to search and replace a pattern on every file.

My goal is to move from this pattern process.env.MY_VAR to env.get('MY_VAR').required() And to move from this pattern process.env.MY_VAR || DEFAULT_VALUE to env.get('MY_VAR').required().default('DEFAULT_VALUE')

For reference, I found this command clear; grep -r "process\.env\." --exclude-dir=node_modules | sed -r -n 's|^.*\.([[:upper:]_] ).*$|\1=|p' > .env.example to generate .env.example

Apparently I can use sed -e "s/pattern/result/" <file list> but I am not sure how to catch the pattern, and return this same pattern in the result.

CodePudding user response:

You have already figured out the main parts of the answer I think. But I'm unclear about what you refer to with MY_VAR. If its actually the name MY_VAR or if its just a dummy name for all var-names consisting of only uppercase characters and underscores. I expect it to be the latter on. Then you could go with something like this:

sed "s/\<process.env.\([A-Z_]*\)\>/env.get('\1').required()/" <file list>

This will read all the files and output them all to stdout with the replacement done. But I guess you should use -i for in-place replacement directly in the file (be careful!).

Since you got several replacements you could give each replacement separately like:

sed -i -e "s/pattern1/result1/" -e "s/pattern2/result2/" <file list>

NOTE: The thing described above could for sure be done in multiple other ways, this is only one solution to my interpretation of your problem!

I would suggest that you take some tutorials on regexp to start of with. It is a handy tool that is present in one form or the other in most programming languages and programming tools (sed being just one such tool).

CodePudding user response:

sed -E '
s/(^|[^[:alnum:]_])process\.env\.([[:alnum:]_] ) \|\| ([[:alnum:]_] )($|[^[:alnum:]_])/\1env.get('\''\2'\'').required().default('\''\3'\'')\4/g
s/(^|[^[:alnum:]_])process\.env\.([[:alnum:]_] )($|[^[:alnum:]_])/\1env.get('\''\2'\'').required()\3/g
' myfile

It's essential that the two substitute commands happen in the above order, because the second pattern also matches the first pattern (which we don't want).

The pattern (^|[^[:alnum:]_]) is just a more portable version of the \< word boundary symbol.

Remember you can use the -i flag with sed to edit the file in place.

Running this on the third paragraph in your question (for example), we get:

My goal is to move from this pattern env.get('MY_VAR').required() to env.get('MY_VAR').required() And to move from this pattern env.get('MY_VAR').required().default('DEFAULT_VALUE') to env.get('MY_VAR').required().default('DEFAULT_VALUE')

  • Related