I have a file which contains text and references to environment variables. Example:
#PRINTME It is always fun to start your week on a sunny ${DAY_OF_WEEK}
#PRINTME My name is ${USERNAME}, you killed my father - prepare to die!
Unrelated gibberish - not to be printed
...
Think of it as metadata.
I want to define a command that greps this file and prints everything marked with #PRINTME and evaluates the environment variables as well.
I did this: grep #HELP myfile | sed "s/#PRINTME //g" | awk '{print $1}' but my output was
It is always fun to start your week on a sunny ${DAY_OF_WEEK}
My name is ${USERNAME}, you killed my father - prepare to die!
Instead of
It is always fun to start your week on a sunny Monday
My name is Inigo Montoya, you killed my father - prepare to die!
Is there a SHELL way to do what I want?
I’m using TCSH – can’t change that.
There are no issues with replacing grep, sed and awk.
You can do it with Gawk:
The script find the variables one by one and replace them by their value.
If you want the OS to evaluate the variables for you, it’s even easier (and doable in standard Awk):
This time, we build an echo command and pass it to a shell.