Wednesday, April 27, 2022

[SOLVED] Extract environment variables from grep search

Issue

I'd like to generate some .env.example files for a dozen of projects.

In order to be smart and avoid tedious copy/pasting I thought about using the grep command.

How can I get from :

➜  app-service git:(chore/adding-env-example) ✗ c;grep -r "process\.env\." --exclude-d
ir=node_modules
./src/config/config_prod.js:    level: process.env.LOG_LEVEL || 'error'
./src/config/config_prod.js:      bypassToken: process.env.BYPASS_TOKEN || null
./src/config/config.js:  port: process.env.APP_PORT || 3000,
./src/config/config.js:  frontUrl: process.env.FRONT_URL,
./src/config/config.js:      launchAppTopic: process.env.AWS_SNS_LAUNCH_APP_ARN
...

to

LOG_LEVEL=
BYPASS_TOKEN=
APP_PORT=
FRONT_URL=
AWS_SNS_LAUNCH_APP_ARN=

?


Solution

Retrieving the Environment Variables:

 grep -oP "(?<=\.)[[:upper:]_]*(?=[ ,]*)" 

Results:

LOG_LEVEL
BYPASS_TOKEN
APP_PORT
FRONT_URL
AWS_SNS_LAUNCH_APP_ARN

Retrieving the Environment Variables and adding =

sed -r -n  's|^.*\.([[:upper:]_]+).*$|\1=|p' 

Results:

LOG_LEVEL=
BYPASS_TOKEN=
APP_PORT=
FRONT_URL=
AWS_SNS_LAUNCH_APP_ARN=


Answered By - Dudi Boy
Answer Checked By - Senaida (WPSolving Volunteer)