print shell variable 
Author Message
 print shell variable

Quote:

> Can I print shell variable in awk ?

> #!\bin\sh
> var=HELLO
> awk '{print $var}'

#! /bin/sh
var=HELLO
awk 'BEGIN{ print "'"$var"'"}'
#   1             234    567 8

Explanation of the numbered quotes:

From 1 to 3: first part of the Awk program
2: a double quote inside the Awk program
From 4 to 5: double quotes for the second part of the Awk program.
   Double quotes because with single quotes the shell would not
   expand [*1*] $var, instead it would give a literal $var to awk.
From 6 to 8: third part of the Awk program.
7: the closing double quote for no. 2.

If you were using gawk, you could use:

export var
awk 'BEGIN{ print ENVIRON["var"]}'

You need to export your variable here to make it visible to programs
called by the shell. If you don't, only the shell sees the variable.
In the first example above, the shell expands the variable, not awk.

*1*: "expanding a variable" means to insert the contents of that
variable.

Regards...
                Michael



Wed, 09 Apr 2003 03:00:00 GMT  
 print shell variable

Quote:


> > Can I print shell variable in awk ?

> > #!\bin\sh
> > var=HELLO
> > awk '{print $var}'

> #! /bin/sh
> var=HELLO
> awk 'BEGIN{ print "'"$var"'"}'
> #   1             234    567 8

Are all those quotes really needed?  Doesn't:

   #!/bin/sh
   var=HELLO
   awk 'BEGIN{ print "'$var'"}'

Do the same thing?



Wed, 09 Apr 2003 03:00:00 GMT  
 print shell variable
Hi,

Can I print shell variable in awk ?

#!\bin\sh
var=HELLO
awk '{print $var}'

Thanks
Gary



Thu, 10 Apr 2003 01:00:05 GMT  
 print shell variable


Quote:


>>>Can I print shell variable in awk ?

>>>#!\bin\sh
>>>var=HELLO
>>>awk '{print $var}'

>>#! /bin/sh
>>var=HELLO
>>awk 'BEGIN{ print "'"$var"'"}'
>>#   1             234    567 8

>Are all those quotes really needed?  Doesn't:

>#!/bin/sh
>var=HELLO
>awk 'BEGIN{ print "'$var'"}'

>Do the same thing?

In this instance, Harry's and Michael's scripts would be equivalent.
However, Michael's version is more robust. If var contained space
characters, e.g., var="Hello, world!", then failing to wrap $var in
double quotes would put two arguments on the awk command line

BEGIN { print "Hello,

and

world!"}

which won't parse as a single command line script under most awks.

It's easier when all shell variables used in shell scripts are wrapped
in double quotes except when one variable _should_ evaluate to multiple
arguments (along with a comment explicitly saying you're doing this).
This may strike many as overkill, but it leads to less time spent on
script debugging.

Sent via Deja.com http://www.deja.com/
Before you buy.



Thu, 10 Apr 2003 11:34:46 GMT  
 print shell variable
Quote:
>Can I print shell variable in awk ?

#!\bin\sh
var=HELLO
awk -v awk_var=$var '{print awk_var}'

It should work in any POSIX compliant awk.
--
HQ



Fri, 11 Apr 2003 03:00:00 GMT  
 print shell variable

Quote:

> >Can I print shell variable in awk ?
> #!\bin\sh
> var=HELLO
> awk -v awk_var=$var '{print awk_var}'

> It should work in any POSIX compliant awk.

Unless var contains any white space.
It is better to enclose the $var in quotes,
i.e.

gawk -v awk_var="$var" 'BEGIN{print awk_var}'

Tristan.



Fri, 11 Apr 2003 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. how to print a shell variable in awk states

2. accessing a shell variable in an awk print statement

3. How to print a shell variable ?

4. awk variables --> shell variables

5. Add shell command to print string

6. AWK-Script to set a shell variable (Win32) ?

7. Passing shell variables to nawk snipet

8. Passing command line variables to AWK in shell script

9. setting shell variables with results from awk

10. shell variable

11. NF and shell variables

 

 
Powered by phpBB® Forum Software