Visit Our UNIX and Linux User Community


reading environment variable from awk script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting reading environment variable from awk script
# 1  
Old 09-23-2009
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:
Code:
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:

Code:
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
# 2  
Old 09-23-2009
You should try awk -v to hand over shell variables.
# 3  
Old 09-23-2009
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.
# 4  
Old 09-23-2009
Question

Quote:
Originally Posted by aigles
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..
# 5  
Old 09-23-2009
Try with nawk instead of awk.


Jean-Pierre.
# 6  
Old 09-23-2009
Data

[/COLOR]
Quote:
Originally Posted by aigles
Try with nawk instead of awk.


Jean-Pierre.

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"

Previous Thread | Next Thread
Test Your Knowledge in Computers #65
Difficulty: Easy
In the TCP/IP model, end-to-end connectivity is provided from host-to-host in the transport layer.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Reading a column from excel using shell script in Linux environment

Hi all I am new to shell scripting. I need to read the 1st column and last of the excel file in linux environment. Can some one help me with examples (3 Replies)
Discussion started by: Vigneshj28
3 Replies

2. Shell Programming and Scripting

Source environment variable in script

Hi, I construct a bash script to finish some tasks. One of the task is to set up some environment variables. There is already one script file to complete the environment variables setting work. I am not able to know the detail of how to set these variables. So, I may just need to call this... (4 Replies)
Discussion started by: gofortime
4 Replies

3. Shell Programming and Scripting

environment variable in shell script called through crontab

Please help me on below.. https://www.unix.com/shell-programming-scripting/141533-retrieve-value-environment-variable-shell-script-called-crontab.html#post302442024 I'm still here. I can still see you! (0 Replies)
Discussion started by: jadoo_c2
0 Replies

4. Shell Programming and Scripting

Retrieve the value of environment variable in shell script which called from crontab

There are two files one is shell script (sample.sh) and another is configuration file (sampl_conf.cfg) configuration file contains one variable $FTP_HOME. the value of this variable vaires for user to user. If user is say jadoo then value is /home/jadoo/ftp/, for user1 - /home/user1/ftp. The... (0 Replies)
Discussion started by: jadoo_c2
0 Replies

5. Shell Programming and Scripting

Setting environment variable using shell script

Hi All, I'm trying to write an menu driven program to automate some functions which involve loging to multiple hosts. The hosts can differ for every use, so I thought I would use an config file to get the hostnames. Now I need to set those values in the config file to environment variable to... (6 Replies)
Discussion started by: arun_maffy
6 Replies

6. UNIX for Dummies Questions & Answers

use AWK to read an Environment variable

Dear Unix gurus, Perhaps I'm missing something,.....I'm having the most infernal difficulty getting AWK to read in an environment variable. For example, value=5 awk '{print ""$value""}' should return the number "5". This is not the case. Can somebody help?:confused: cheers (7 Replies)
Discussion started by: tintin72
7 Replies

7. UNIX for Dummies Questions & Answers

setting environment variable in awk

Dear all, I have a data sample... Dose: Summed ROI: Bladder ************************** Bin Dose Volume 001 0.700 100.000 002 0.715 99.998 168 3.142 0.368 169 3.157 0.338 170 3.171 0.292 Dose: Summed ROI:... (2 Replies)
Discussion started by: tintin72
2 Replies

8. Shell Programming and Scripting

Simple script to return environment variable

HI , In the below script I am trying to return the value of the environment variable TIBCO_HOME to the caller #! /usr/bin/csh set VAR_NAME=$1 echo VAR_NAME On the aix console.. set to setenv TIBCO_HOME /app/tibco When I execute the script... myscript.sh TIBCO_HOME, the script... (5 Replies)
Discussion started by: bce_groups
5 Replies

9. Shell Programming and Scripting

Setting environment variable on a remote solaris machine using shell script

Hi, I am trying to set environment variable on a remote machine. I want to do it by running a shell script Here's what I am doin rsh <remote-hostname> -l root "cd /opt/newclient; . ./setp.sh" In setp.sh, I have ############################# cd ../newlib; export... (1 Reply)
Discussion started by: eamani_sun
1 Replies

10. Shell Programming and Scripting

problem in getting the path of environment variable set in bashrc in my shell script

hi all i have joined new to the group. i have set an variable in my bashrc file. .bashrc PROGHOME=/home/braf/braf/prog export PROGHOME but while using it in my shell script its path is not taken and i had to explicitly give the export command to set the path. in my script... (8 Replies)
Discussion started by: krithika
8 Replies

Featured Tech Videos