Re: Shell Substitution When Reading From File
From: laura fairhead (laura_fairhead_at_btinternet.com)
Date: 08/25/03
- Next message: laura fairhead: "Re: How to delete the lines by sed"
- Previous message: laura fairhead: "Re: How to rename a set of files according their names?"
- In reply to: abend: "Shell Substitution When Reading From File"
- Next in thread: John W. Krahn: "Re: Shell Substitution When Reading From File"
- Reply: John W. Krahn: "Re: Shell Substitution When Reading From File"
- Reply: abend: "Re: Shell Substitution When Reading From File"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: laura fairhead: "Re: How to delete the lines by sed"
- Previous message: laura fairhead: "Re: How to rename a set of files according their names?"
- In reply to: abend: "Shell Substitution When Reading From File"
- Next in thread: John W. Krahn: "Re: Shell Substitution When Reading From File"
- Reply: John W. Krahn: "Re: Shell Substitution When Reading From File"
- Reply: abend: "Re: Shell Substitution When Reading From File"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|