LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Shell variable in awk script (https://www.linuxquestions.org/questions/programming-9/shell-variable-in-awk-script-790765/)

rohini_sharma 02-22-2010 08:01 AM

Shell variable in awk script
 
I need to export some Varibales in Shell like (KSH )

export CORE=1
export DODDY=0

then I need to call these variables in the awk

#!/bin/awk -f

BEGIN {

if ( $CORE ~ "1" ) {

print "Core is right one "

}

But awk doesnot understand the variables and I am not getting the right results

Can you help me out

Thanks
Rohini

catkin 02-22-2010 08:15 AM

Try awk's -v option

irmin 02-22-2010 08:17 AM

In awk the environment variables can be accessed through the associative array ENVIRON:
Code:

#!/bin/awk -f

BEGIN {

if ( ENVIRON["CORE"] ~ "1" ) {

print "Core is right one "

}


pixellany 02-22-2010 08:32 AM

Quick glance:

The -f flag says to get code from a file, but you have the code inline.

"~" means "ERE match". Why not just use "==" ?


All times are GMT -5. The time now is 04:20 PM.