A Mehmeto
A Mehmeto

Reputation: 1999

Extract environment variables from grep search

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=

?

Upvotes: 1

Views: 3627

Answers (2)

Dudi Boy
Dudi Boy

Reputation: 4890

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=

Upvotes: 1

Andy Lester
Andy Lester

Reputation: 93715

If you want to find all occurrences of an uppercase letter followed by uppercase letters and/or underscores, and then an =, and only if it is a word in itself:

grep -o -P '\b[A-Z][A-Z_]*='

Upvotes: 0

Related Questions