reading environment variable from awk script

Hi All,

I am using SunSolaris machine.

I need to get the value of environment variable from awk begin. Then use that value as the start number of a sequence and use that in my print statement. But it is not reading the value from environment variable.

I have tried the following:

Calling script:

echo Starting
awk -f generatesequence.awk $1 > $2.csv
echo finish

Note: $1 is the input file to be read and $2 is the output file the output is redirected

Called script:

BEGIN {
      FS="\""
x=0
STR="Starting_string"
VAR=ENVIRON["HOME"]
}
/^Event/ { x=x+1 }
/^Event/ { VAR=VAR+1 }
/^Event/ { print STR","x","$2","VAR }

Note: I am getting blank value into variable VAR even though I export the environment variable HOME in UNIX as "export HOME=100".

I am getting the output file data as
Starting_string,1,0667181535,1
Starting_string,2,0667181535,2
Starting_string,3,0667181535,3

But I expect it to come something as
Starting_string,1,0667181535,101
Starting_string,2,0667181535,102
Starting_string,3,0667181535,103

Thanks in advance.
prashas_d

You should try awk -v to hand over shell variables.

Using the variable HOME is not a good idea since this variable is set to the home directory by the shell.

For debug purpose, print the value of VAR in the BEGIN pattern.
If the value of VAR is not numeric, the first time you add one to VAR the variable will be set to 1 (non numeric value is trated as zero).

Jean-Pierre.

Thanks Jean-Pierre..

I have tried to set some env variable and but not being able to access it through ENVIRON..

Try with nawk instead of awk.

Jean-Pierre.

[/COLOR]

I am unable to use nawk inside the awk script

getting the following error on the line where i used nawk
"awk: syntax error near line 15
awk: bailing out near line 15"