Help with a simple script using awk

I need a hand with this simple script,
in Unix i have a variable called portal:
$ echo $portal
chu0
when i use awk this variable is not recognized. How can i make awk recognize and print the value of this variable, since the output is not shown i.e.

awk '
BEGIN {printf("%4s\t%14s\n", "Portal:", portal)}' listacdrs
output:Portal:
Thnaks.

Once you enter awk, you cannot use the shell variables directly using $portal or some such. This is because awk has no access to shell variables.

You can pass variables to awk using the '-v' switch. Something like this:

nawk -v a=$a '{print a}' filename

The above code serves no purpose other than to show how a shell variable can be passed to awk. Note that the original Solaris awk does not support this, so with Solaris, you have to use nawk.

You can also define the variable in the following way :

awk '{print a}' a=$a filename

The difference is that in that case the variable a is not define in the BEGIN clause.

The script

awk -v v_var='v_option' \
    'BEGIN  { printf("BEGIN v_var=%s f_var=%s\n", v_var, f_var)}
     FNR==1 { printf("FILENAME=%s v_var=%s f_var=%s\n", FILENAME, v_var, f_var)}
     END    { printf("END   v_var=%s f_var=%s\n", v_var, f_var)}' \
     f_var='files_1_2' file1 file2 \
     f_var='file_3'    file3

gives the output :

BEGIN v_var=v_option f_var=
FILENAME=file1 v_var=v_option f_var=files_1_2
FILENAME=file2 v_var=v_option f_var=files_1_2
FILENAME=file3 v_var=v_option f_var=file_3
END   v_var=v_option f_var=file_3

Jean-Pierre.

Actually, u can use shell variables inside awk.
for e.g.

u have the file abc which has the folloing contents :

abc def ghi
jkl mno pqr

u need to print the 3rd column
create the shell variable
x=3

then

awk '{print $'$x'}' abc

u'll get the required output. what u need to do is to switch off quoting before $x so that the shell can evaluate it (n return 3) n then switch it on again.

so in ur case, simply, instead of portal, write '$portal'