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