Skip to content
Advertisement

How to add a word at the beginning of each line BUT not when a # is present

Given a file with many ENV variables.

Example: DEV.env

TRUST_STORE=/run/secret/truststore.jks
#TRUST_STORE_PASSWORD=changeit
TRUST_STORE_PASSWORD=ch@nge1t

I know the command sed "s/^/export /g" will add export at the beginning of each line.

But I don’t want to do this when the first character is “#”.

How can I do with sed?

Advertisement

Answer

Something like this:

$ sed "s/^[^#]/export &/g"
export TRUST_STORE=/run/secret/truststore.jks
#TRUST_STORE_PASSWORD=changeit
export TRUST_STORE_PASSWORD=ch@nge1t

Alternatives:

$ sed "/^[^#]/ s/^/export /g"
$ sed "/^#/n; s/^/export /g"  #n : read the next line
Advertisement