Re: Shell Substitution When Reading From File

From: laura fairhead (laura_fairhead_at_btinternet.com)
Date: 08/25/03


Date: Mon, 25 Aug 2003 15:45:34 GMT

On 21 Aug 2003 23:32:57 -0700, abend@canada.com (abend) wrote:

>Hi all,
>
>I'm figuring there must be some sort of relatively "easy" way to do
>this... :)
>
>I have a text file whose contents contains shell variable names that I
>would like to expand upon reading the file. e.g., myfile.txt:
>
> ...
> These are the contents of ${USER}'s nifty file.
> ...
>
>I'd like to read in this text file (somehow) -- with shell variable
>substitution performed -- and write it back out to another file, so
>that the output file's contents are:
>
> ...
> These are the contents of abend's nifty file.
> ...
>
>Any ideas?

The way I would do this is to use a small 'awk' program.

'awk' will let you read the environment variables using
the ENVIRON[] array, so you can just find every occurance
of ${variable name} and replace that with the contents
something like;

{
s=$0
t=""
for(;match(s,"\\$[{][^}]*[}]");s=substr(s,RSTART+RLENGTH,9999))
  {
  t=t""substr(s,1,RSTART-1)""ENVIRON[substr(s,RSTART+2,RLENGTH-3)]
  }
print t""s
}

>
>Thanks much...

byefrom

l



Relevant Pages

  • Re: windows scripting with gawk
    ... gawk script but it is not effective ???? ... ENVIRONarray within awk. ... COPY of the environment variables names and associated values when awk ...
    (comp.lang.awk)
  • Re: windows scripting with gawk
    ... gawk script but it is not effective ???? ... ENVIRONarray within awk. ... COPY of the environment variables names and associated values when awk ... Note that "call" is used if in a batch file and further batch statements are present since a batch file is immediately exited without the "call". ...
    (comp.lang.awk)
  • Re: Shell Substitution When Reading From File
    ... Perhaps I'll give your approach a shot too then. ... Jason ... > 'awk' will let you read the environment variables using ...
    (comp.unix.shell)