using /bin/sh vars in awk

using /bin/sh vars in awk

Post by gasp » Wed, 03 Apr 1991 21:14:26



ok, I know this came up awhile ago, and I dont want to bring it
up again.  However, not enuf disk to keep articles around, and
I forgot the answer:

Question: what syntax do I have to use to get awk to take /bin/sh
          variables in a shell script?

Example:  given a /bin/sh script, and an awk command in it somewheres,
          and i need to pass the var ${fred} to awk, what should
          the following line look like?

          fred="barney"
          wilma=`cat ${file1} | awk '{if($1== fred ){print("match")}}'
                                              ^^^^
             what I need is how to expand fred_|  to get awk to
          replace it with the string "barney"

Since this already came up, just email me the answer.  I've already
tried ${fred}, $fred, "${fred}" and a couple other combos.

thanks

--gaspo.

BTW, the example is just that, I don't need a different way to solve
the example.  just get the vars to awk.

/----------------------------------------------------------------------------\
| Scott "gaspo" Gasparian  --  System Administrator | _>________  _<________ |
| Dept. Informatik, Eidg. Techn. Hochschule, Zurich |/[][][][][]\/[][][][][]\|
| ETH-Zentrum,  CH-8092  Zurich.  T# 01-01-254-7205 |`oo------oo'`oo------oo'|

| ..!ethz-inf!gaspar | along the way.In this proud land,you can't forget your|

\----------------------------------------------------------------------------/

 
 
 

using /bin/sh vars in awk

Post by andr » Mon, 22 Apr 1991 05:41:19



  >Question: what syntax do I have to use to get awk to take /bin/sh
  >          variables in a shell script?

  >Example:  given a /bin/sh script, and an awk command in it somewheres,
  >          and i need to pass the var ${fred} to awk, what should
  >          the following line look like?

  >          fred="barney"
  >          wilma=`cat ${file1} | awk '{if($1== fred ){print("match")}}'
  >                                              ^^^^
  >             what I need is how to expand fred_|  to get awk to
  >          replace it with the string "barney"
  >
  >Since this already came up, just email me the answer.  I've already
  >tried ${fred}, $fred, "${fred}" and a couple other combos.

Your problem is that you put the wak problam inside single ' quotes
and the shell will not expand variables in there no matter how you
try. (that's the good thing about single quotes).

If you want to expand a variable inside such a string, you must
end the quoted part, expand the variable and start the quotation
again. To be sure that it stays 1 argument if there are spaces
in the variable use double quotes to keep them together.

In your case this becomes:

  wilma=`cat ${file1} | awk '{if($1== '"$fred"' ){print("match")}}'

        hope this helps
--
The mail|    AAA         DDDD  It's not the kill, but the thrill of the chase.
demon...|   AA AAvv   vvDD  DD        Ketchup is a vegetable.

--more--| AAA   AAAvvvDDDDDD    Andre van Dalen, uunet!hp4nl!targon!andre

 
 
 

using /bin/sh vars in awk

Post by Randy Bu » Tue, 23 Apr 1991 06:26:12


#! /bin/sh
#
awk "{if (substr(\$1,1,3)==192) {print \"$1 \" \$2 \" $2 $3 $4 $5 $6 $7 $8 $9\" }}" /etc/hosts | sort | /bin/sh -v

--

 
 
 

using /bin/sh vars in awk

Post by Watanabe Ma » Wed, 01 May 1991 09:58:59



>   >Question: what syntax do I have to use to get awk to take /bin/sh
>   >          variables in a shell script?

>   >Example:  given a /bin/sh script, and an awk command in it somewheres,
>   >          and i need to pass the var ${fred} to awk, what should
>   >          the following line look like?

>   >          fred="barney"
>   >          wilma=`cat ${file1} | awk '{if($1== fred ){print("match")}}'
>   >                                              ^^^^
>   >             what I need is how to expand fred_|  to get awk to
>   >          replace it with the string "barney"

>   >Since this already came up, just email me the answer.  I've already
>   >tried ${fred}, $fred, "${fred}" and a couple other combos.

        If your awk is nawk(New Awk), it's easy.

        ... | nawk '{if ($1 == fred) command}' fred="barney"

m.      
--
    Watanabe,Maki
    Finance/EIC
    Digital Equipment Corporation Japan

 
 
 

using /bin/sh vars in awk

Post by Jonathan I. Kame » Tue, 30 Apr 1991 21:27:14


|>   If your awk is nawk(New Awk), it's easy.
|>  
|>   ... | nawk '{if ($1 == fred) command}' fred="barney"

  Actually, this syntax will work with nearly any version of awk, not just
nawk.  It was available, albeit undocumented, in even the earliest distributed
versions.

--
Jonathan Kamens                               USnail:
MIT Project Athena                              11 Ashford Terrace

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

 
 
 

using /bin/sh vars in awk

Post by Martin Weitz » Thu, 02 May 1991 22:23:24




>|>       If your awk is nawk(New Awk), it's easy.
>|>      
>|>       ... | nawk '{if ($1 == fred) command}' fred="barney"

>  Actually, this syntax will work with nearly any version of awk, not just
>nawk.  It was available, albeit undocumented, in even the earliest distributed
>versions.

Minor nitpick: But be sure to add a "-" if you want to have stdin processed.

        ... | nawk '{if ($1 == fred) command}' fred="barney" -
        # Important little thing here, watch closely! -------^

Reason: awk in its older versions is a bit stupid about the command line
arguments that are assignments to awk-variables and counts them as if they
were file arguments, failing process stdin as default if there are any
such assignments but no file arguments. The trick with explicitly naming
stdin solves this problem.
--

 
 
 

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

3. SUMMARY: using sh vars in awk calls.

4. Help to my IBM laptop! Install Solaris 8

5. /usr/bin, /usr/local/bin, /sbin or /opt/bin, /var/opt/bin - I'm confused.

6. UPS on H50

7. #!/bin/sh #!/usr/bin/sh can I do both for 2 diff machines

8. Upgrading on linux

9. more secure?: "#!/bin/sh -" or "#!/bin/sh"

10. Q: Sendmail Bug using From: |/bin/tail|/bin/sh

11. Maintaining white space in vars (/bin/sh)

12. /bin/sh: VAR=function arg arg ? (possible?)

13. sh vars in awk scripts??