SUMMARY: using sh vars in awk calls.

SUMMARY: using sh vars in awk calls.

Post by gasp » Sat, 06 Apr 1991 16:42:32



greetings all!

Ok, thanks to all who replied, and since I got a couple "me toos",
heres the deal:

Problem:
        in a /bin/sh script, need to pass a regular sh variable
        to the awk call, but the proper syntax is not-readily
        discernable.

Example:
        given sh vars $fred, call awk using fred, like so:

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

Solution:
        (1), escape out the sh variable by using single quotes.


Quote:

>     .... awk '{ ... '$fred' ...}' ...

>Since single-quotes suppress variable substitution, if you want the shell
>to expand the reference you have to leave it outside.  Note that
>the whole awk argument is still treated as one word by the shell, since
>there is no white space between the single quotes and the expanded shell
>variable.


        different syntax of

Quote:

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

        (2), the other possible way is to declare awk a variable after
        the end of the awk script (in the man pages, but I cant find it)
        like so:


Quote:>            wilma=`cat $file1 | awk '
>            $1 == fredvar {printf("match on %s\n",fredvar)}
>            ' fredvar="$fred"`


Quote:>    fred="barney"
>    wilma=` cat ${file1} |
>        awk '{
>                if ($1 == fred {print("match")}
>             }' fred=${fred} - `

>Awk allows command line assignments of the form 'name=value'.  These
>assignments follow the awk script and precede the filenames.  In most
>versions of awk, you must have at least one filename on the command
>line.  If you want awk to read from standard input (as in the above
>example), you can use a filename of '-'.

I've only tried the single quote method, and it works fine.  Some of
this is supposed to be dependent on your version of awk.  dont ask,
i dont know.

Thanks for all the info!

--gaspo.

BTW, to the dude who thought my sig was "long and obnoxious", you wouldnt
like my hair either!

/----------------------------------------------------------------------------\
| Scott "gaspo" Gasparian  --  System Administrator |"Do you go every Sunday?|
| Dept. Informatik, Eidg. Techn. Hochschule, Zurich | sounds to me like YOU  |
| ETH-Zentrum,  CH-8092  Zurich.  T# 01-01-254-7205 | are the *ed one." |

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

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

 
 
 

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. changing default shell

3. using /bin/sh vars in awk

4. XFree86 not starting

5. sh vars in awk scripts??

6. Help decode base64

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

8. VNC and KDE

9. sh script using awk, argument issue?

10. awk: -v Var=VAR Obviously not a syntax issue

11. Using awk and sh variables

12. sh & awk: Quoting Awk Syntax in Bourne shell?

13. using a c program to call a sh script