It is currently Fri, 15 Oct 2021 21:31:16 GMT



 
Author Message
 sh vars in awk scripts??
Hi,
        Is there any way to embed sh environment variables in an awk script??

        eg. user=me;awk '{ print $1, ${user} }' .rhosts

        I know there is a way but be beggered if I can remember it or find it.

        Heres hoping .....Ta.....MarkC.....

--
Mark Coleman                   O   O   EMAIL: colem...@cheops.qld.tne.oz.au
Telecom Australia                |     FAX:   +61 7 837 4704
TNE Computer Support Services  *****   PH:    +61 7 837 3143
Brisbane, Queensland. OZ       \===/   "You'll find these are my opinions alone"



 Mon, 28 Feb 1994 09:31:14 GMT   
 sh vars in awk scripts??
In article <1991Sep12.013114.20...@cheops.qld.tne.oz.au>, colem...@cheops.qld.tne.oz.au (Mark Coleman) writes:
|>   Is there any way to embed sh environment variables in an awk script??

Yes.  At least two different ways, in fact.

|>   eg. user=me;awk '{ print $1, ${user} }' .rhosts

This doesn't work, simply because shell variable substitution does not take
place inside single quotes.  That is, after all, *why* you are using the
single quotes -- to prevent the '$' character from being interpreted as the
sign of an impending shell variable name, so that you can use it in awk
expressions.

The command above would work if you did:

        user=me; awk '{print $1, '"$user"' }' .rhosts

The double quotes around $user are not strictly necessary, because it's
probably going to be only one word long and not contain any special shell
characters, but I put them there because you may at some point may want to
feed a variable with spaces and/or special characters into awk.

The other way to pass values in is to use a often undocumented feature of awk:

        awk '{print $1, user }' user="$user" .rhosts

In addition to being undocumented, this feature is sometimes inconsistent on
different versions of awk -- some versions will define the values you specify
before the BEGIN block is executed, and some won't define the values until
after BEGIN.

Note, further, that when you specify a variable setting as a "fake file name"
as I've indicated above, awk (or, at least, some versions of it) won't
automatically read from stdin if no real files are specified, so you need to
add a "-" argument to the end of your command if you want awk to read stdin.

--
Jonathan Kamens                               USnail:
MIT Project Athena                              11 Ashford Terrace
j...@Athena.MIT.EDU                         Allston, MA  02134
Office: 617-253-8085                          Home: 617-782-0710



 Tue, 01 Mar 1994 03:26:04 GMT   
 sh vars in awk scripts??
|>   user=me; awk '{print $1, '"$user"' }' .rhosts

As s.desmarais pointed out in his posting, this is wrong, because quotes are
needed around the username inside the awk program.  This should read:

        user=me; awk '{print $1, "'"$user"'" }' .rhosts

--
Jonathan Kamens                               USnail:
MIT Project Athena                              11 Ashford Terrace
j...@Athena.MIT.EDU                         Allston, MA  02134
Office: 617-253-8085                          Home: 617-782-0710



 Tue, 01 Mar 1994 03:32:37 GMT   
 sh vars in awk scripts??
In <1991Sep12.013114.20...@cheops.qld.tne.oz.au> colem...@cheops.qld.tne.oz.au (Mark Coleman) writes:

I know of 2 ways: play tricks with ' and ", or use the "parameters" field
of the call to awk:

        user=me ; awk '{ print $1, "'${user}'" }' .rhosts
                      ^-------------^^-----^^---^
                                   ^-- this double-quote is needed because
                 you want to print the string "me", not the awk variable me.

What this does is get out of the quote mode just long enough to get
back to the shell, access the value, and go back to quote mode.
By the way, it the ${user} variable contains spaces or some other
special characters, you would need to enclose it in "":

        user=me ; awk '{ print $1, "'"${user}"'" }' .rhosts
                      ^-------------^^-------^^---^

The other way is:

        user=me ; awk '{ print $1, awkuser }' awkuser=${user} .rhosts

This initializes the variable "awkuser" as if it would be in a BEGIN.

--
Stephane M. Desmarais               sdesm...@sobeco.com  ou  sdesm...@sobeco.ca
Division STS - Groupe Sobeco Inc.   {uunet | mcgill-vision}!sobeco.com!sdesmara
505 boul Rene-Levesque Ouest        bur: (514) 878-9090 poste 297
Montreal, Quebec CANADA H2Z 1Y7     fax: (514) 875-2673  dom: (514) 733-3245



 Tue, 01 Mar 1994 02:57:04 GMT   
 sh vars in awk scripts??
From article <1991Sep12.013114.20...@cheops.qld.tne.oz.au>, by colem...@cheops.qld.tne.oz.au (Mark Coleman):

Try the following:

awk '{ print $1, x }' x=me .rhosts

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    Stephen {*filter*}iuk           |   E-mail: ...!alberta!edm!stephen
    Sentai Software Corp.      |           step...@edm.UUCP
    10219 - 112 St.            |    Voice: (403) 423 - 3113
    Edmonton, Alberta, Canada  |      FAX: (403) 425 - 8003
    T5K 1M7                    |   Humour: Dwn wth vwls!



 Tue, 01 Mar 1994 01:19:38 GMT   
 sh vars in awk scripts??
In article <1991Sep12.013114.20...@cheops.qld.tne.oz.au>, colem...@cheops.qld.tne.oz.au (Mark Coleman) writes:
|> Hi,
|>   Is there any way to embed sh environment variables in an awk script??
|>
|>   eg. user=me;awk '{ print $1, ${user} }' .rhosts

try awk '{print $1, '${user}' }' .rhosts

always happy to help a good customer like Telecom Australia.

Michael Salmon
not speaking but working for
Ericsson Telecom AB
Stockholm



 Mon, 28 Feb 1994 18:53:02 GMT   
 sh vars in awk scripts??
In article <1991Sep12.013114.20...@cheops.qld.tne.oz.au> colem...@cheops.qld.tne.oz.au (Mark Coleman) writes:

This should be in a FAQs list somewhere...   :-)

        :
        # Sample sh or ksh script passing the variable $me
        # into an awk or nawk script

        user=me

        awk '{print $1, user}' user="$user" .rhosts

...Kris
--
Kristopher Stephens, | (408-746-6047) | k...@uts.amdahl.com | KC6DFS
Amdahl Corporation   |                |                    |
     [The opinions expressed above are mine, solely, and do not    ]
     [necessarily reflect the opinions or policies of Amdahl Corp. ]



 Wed, 02 Mar 1994 00:37:08 GMT   
 
   [ 7 post ] 

Similar Threads

1. using /bin/sh vars in awk

2. SUMMARY: using sh vars in awk calls.

3. environment vars in sh script

4. env./csh vars. within a sed/awk script

5. awk processing in sh script

6. awk and sh variables in a shell script

7. awk and sh variables in a shell script

8. Passing a string Varible from AWK to a sh Script

9. sh script using awk, argument issue?

10. sh: read vars from file


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