sh vars in awk scripts??

sh vars in awk scripts??

Post by Mark Colem » Fri, 13 Sep 1991 10:31:14



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.....

--

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"

 
 
 

sh vars in awk scripts??

Post by Jonathan I. Kame » Sat, 14 Sep 1991 04:26:04



|>   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

Office: 617-253-8085                          Home: 617-782-0710

 
 
 

sh vars in awk scripts??

Post by Jonathan I. Kame » Sat, 14 Sep 1991 04:32:37


|>   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

Office: 617-253-8085                          Home: 617-782-0710

 
 
 

sh vars in awk scripts??

Post by s.desmara » Sat, 14 Sep 1991 03:57:04



>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.
>--


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.

--

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

 
 
 

sh vars in awk scripts??

Post by Stephen Semeni » Sat, 14 Sep 1991 02:19:38



> 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.....

> --

> 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"

Try the following:

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

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    Stephen *iuk           |   E-mail: ...!alberta!edm!stephen

    10219 - 112 St.            |    Voice: (403) 423 - 3113
    Edmonton, Alberta, Canada  |      FAX: (403) 425 - 8003
    T5K 1M7                    |   Humour: Dwn wth vwls!

 
 
 

sh vars in awk scripts??

Post by Michael Salm » Fri, 13 Sep 1991 19:53:02



|> 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

 
 
 

sh vars in awk scripts??

Post by Kris Stephens [Hail Eris » Sun, 15 Sep 1991 01:37:08



Quote:>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.....

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
--

Amdahl Corporation   |                |                    |
     [The opinions expressed above are mine, solely, and do not    ]
     [necessarily reflect the opinions or policies of Amdahl Corp. ]

 
 
 

1. sh var -> awk var -> sh var

I want to do something like the following:
---------------------------------------------------------------------
echo "-----------end----------------"
line_num=`expr $line_num + 1`
awk '{  
       for( ; n%=xx ; n++ )  <---xx means a number.
        printf("\n")
     }' n=$page_line          <---this need input, else it stop....
line_num=$n    <-----I want $line_num = n in awk.
---------------------------------------------------------------------
I need certain length of the outfiles, so if "----end----" end at line number
which is not enough, space lines are added to enough length number.
Thanks.

--
  =====================================================================
    Simon Tneoh Chee Boon  ~{UEV>ND~}  i?

    Hsin Chu City       NATIONAL TSING HUA UNI.
    P.O.Box 2-037       Material Science Engineering Dept. Batch '96

    R.O.China.          http://140.114.63.14:6083/simon.html
    886-35-715131-7300
  =====================================================================

2. New to RH7 X-term help needed

3. Execute a sh script under perl and sh: sh script; perl script?

4. Pipe in X term

5. Question: passing $var and "$var" into AWK from script

6. Matrox Mystic X and Hires Textconsoles (second try)

7. using /bin/sh vars in awk

8. Kernel change summary 1.1.53 -> 1.1.54

9. SUMMARY: using sh vars in awk calls.

10. sh script using awk, argument issue?

11. awk and sh variables in a shell script

12. awk processing in sh script

13. awk and sh variables in a shell script