It is currently Tue, 30 Nov 2021 18:32:34 GMT



 
Author Message
 env./csh vars. within a sed/awk script
How can I use an environment/shell variables within an awk/sed script?
Whenever I use ${VAR} it prints it out literally, instead of substituting
the value of the variable.  I'm guessing the ' is "escaping" the
variable, but I want it "unescaped."

The example I'm having problems with is:
        #!/bin/csh
        set UNL_SRC = /usr7/prod/cic/doc
        [...]
        awk -F" " '{printf("${UNL_SRC}/%s\n",$0)}' ${UNL_LIST} >
                ${UNL_LIST}.path

It prints out:
        ${UNL_SRC}/relative_path_on_line1
        ${UNL_SRC}/relative_path_on_line2
        ${UNL_SRC}/relative_path_on_line3
instead of the desired:
        /usr7/prod/cic/doc/relative_path_on_line1
        /usr7/prod/cic/doc/relative_path_on_line2
        /usr7/prod/cic/doc/relative_path_on_line3

I've tried also:
        awk -F" " '{printf("%s/%s\n",${UNL_SRC},$0)}' ${UNL_LIST} >
                ${UNL_LIST}.path
which does the same thing, printing out the variable name, not the
value.

I tried using sed, e.g.:
        sed 's/^/${UNL_SRC}/' ${UNL_LIST} > ${UNL_LIST}.path
but it still prints out the literal ${UNL_SRC}, instead of it's value.

Help.  All I want to do is prepend each line with the variable.  If
somebody's got a fix, or can suggest an alternate method, I'd appreciate
it.

        Until next time...
                                        Todd
                                        to...@loki.stanford.edu

Famous last words:  What happens if you touch these two wires tog--
---



 Mon, 16 Dec 1996 23:50:22 GMT   
 env./csh vars. within a sed/awk script
In article <2uupju$...@nntp2.Stanford.EDU>,
Todd C. Merrill <to...@loki.stanford.edu> wrote:

Exactly.  The single quotes prevent the shell from interpreting the variable
that is enclosed by them.

change your awk statement to the following:

   awk -F" " '{printf("'${UNL_SRC}'/%s\n",$0)}' ${UNL_LIST} > ${UNL_LIST}.path
             ^^^^^^^^^^^          ^^^^^^^^^^^^^
The parts enclosed in single quotes are not interpreted by the shell, while
the variable part, which is not enclosed by single quotes, is interpreted by
the shell.

This one would be more fun, because your variable has slashes in it.  To do
this one with sed, try:

        sed "s?^?${UNL_SRC}/?" ${UNL_LIST} > ${UNL_LIST}.path

Shell variables are still interpreted within double quotes.
The ? is used as sed's command separator so that /'s can be used in the
replacement pattern.  (I chose ? because it's highly unlikely to occur
in a filename).  The extra slash was added because it looks like that's
what you want.

Have fun.  For more tricks, try the O'Reilly book "Sed and Awk"

==============================================================================
Sean "Yoda" Rouse   Cal '90   |  "...and enjoy the rest of your day at the
y...@soda.berkeley.edu        |   largest human trap ever built by a mouse."
...!ucbvax!soda!yoda          |       -- Yet another Jungle Cruise joke



 Tue, 17 Dec 1996 02:09:30 GMT   
 env./csh vars. within a sed/awk script
#!/bin/csh
set COMMENT = "REAL SysOps use /bin/sh ;)"
echo " " | awk '{ printf("%s\n", "'"$COMMENT"'") }'

In article <2uupju$...@nntp2.Stanford.EDU>, to...@loki.stanford.edu (Todd C. Merrill) writes:
|> How can I use an environment/shell variables within an awk/sed script?
|> Whenever I use ${VAR} it prints it out literally, instead of substituting
|> the value of the variable.  I'm guessing the ' is "escaping" the
|> variable, but I want it "unescaped."
|>
|> The example I'm having problems with is:
|>   #!/bin/csh
|>   set UNL_SRC = /usr7/prod/cic/doc
|>   [...]
|>   awk -F" " '{printf("${UNL_SRC}/%s\n",$0)}' ${UNL_LIST} >
|>           ${UNL_LIST}.path
|>
|> It prints out:
|>   ${UNL_SRC}/relative_path_on_line1
|>   ${UNL_SRC}/relative_path_on_line2
|>   ${UNL_SRC}/relative_path_on_line3
|> instead of the desired:
|>   /usr7/prod/cic/doc/relative_path_on_line1
|>   /usr7/prod/cic/doc/relative_path_on_line2
|>   /usr7/prod/cic/doc/relative_path_on_line3
|>
|> I've tried also:
|>   awk -F" " '{printf("%s/%s\n",${UNL_SRC},$0)}' ${UNL_LIST} >
|>           ${UNL_LIST}.path
|> which does the same thing, printing out the variable name, not the
|> value.
|>
|> I tried using sed, e.g.:
|>   sed 's/^/${UNL_SRC}/' ${UNL_LIST} > ${UNL_LIST}.path
|> but it still prints out the literal ${UNL_SRC}, instead of it's value.
|>
|> Help.  All I want to do is prepend each line with the variable.  If
|> somebody's got a fix, or can suggest an alternate method, I'd appreciate
|> it.
|>
|>   Until next time...
|>                                   Todd
|>                                   to...@loki.stanford.edu
|>
|> Famous last words:  What happens if you touch these two wires tog--
|> ---

--
dfruss...@unixmail.rtpnc.epa.gov; MMTSI UGTS, at the EPA
DISCLAIMER: I don't speak for the EPA or Martin Marietta.
-----------------------------------------------------------
Tiger! Tiger! burning bright, in the forests of the night,
What immortal hand or eye, could frame thy fearful symmetry.



 Tue, 17 Dec 1996 22:07:44 GMT   
 env./csh vars. within a sed/awk script
Sean "Yoda" Rouse (y...@soda.berkeley.edu) wrote:

You can also hand over parameters to awk on the command line. The awk
command line is (in your case)

 awk -Fc [prog] [parameters] [filename]

You can specify parameters in the form Name1=Value1 Name2=Value2 etc.
I'm not sure if it's required, but I always use the environment
variables first in the BEGIN section of the awk program. In the BEGIN
section, you can assign default values to these parameters; if you specify
different values on the command line, these are used.
BTW: Use 'nawk' if you can. There, you specify variables on the command
line with the -v option.

chr

---------------------------------------------------------------------------
Christian Veith              |             c...@catwoman.zfe.siemens.de
Siemens AG, Munich, Germany  |             +49-89-636-44519
---------------------------------------------------------------------------



 Tue, 17 Dec 1996 22:40:48 GMT   
 
   [ 4 post ] 

Similar Threads

1. csh test for large env vars?

2. Limit to number of Env Vars in Csh?

3. Env Vars and sed

4. csh commands within csh scripts

5. Changing env. vars from script

6. Setting Env Vars on Server per CGI script

7. Using system and sed within awk

8. csh vars questions: URL passed to IE (on Solaris) from script

9. Anyway to pass vars betwen scripts in Csh?


 
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software