use AWK to read an Environment variable


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers use AWK to read an Environment variable
# 1  
Old 06-09-2009
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,

Code:
 
value=5
awk '{print ""$value""}'

should return the number "5". This is not the case. Can somebody help?Smilie

cheers
# 2  
Old 06-09-2009
Code:
value=5
awk -v val=$value '{print val}'  anyfile

where anyfile is just a small file that exists

Last edited by jim mcnamara; 06-09-2009 at 01:26 PM..
# 3  
Old 06-09-2009
Quote:
Originally Posted by jim mcnamara
Code:
value=5
awk -v val=$value '{print val}'


doesn't work. my screen hangs.
# 4  
Old 06-09-2009
It hung because awk expected input from stdin. My bad. See the original post above
# 5  
Old 06-09-2009
nice! it works.

thanks
# 6  
Old 06-10-2009
Good One Jim Smilie

If you dont want to make use of any file, you can try using echo command:

e.g.
echo abc | awk -v val=$HOME '{print val}'

In your case:

echo abc | awk -v val=$value '{print val}'
# 7  
Old 06-10-2009
thanks.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to read the variable from awk output?

I am reading an xml file with date tag as <Date>Default</Date> using the below command. Dt=$(awk -F'' '/<Date>/{print $3}' /home/test/try.xml and getting the value from the xml file stored in this variable "Dt" echo $Dt gives me a value. Dt=Default. Now according to my requirement, If... (2 Replies)
Discussion started by: Saidul
2 Replies

2. Shell Programming and Scripting

How to read a two files, line by line in UNIX script and how to assign shell variable to awk ..?

Input are file and file1 file contains store.bal product.bal category.bal admin.bal file1 contains flip.store.bal ::FFFF:BADC:CD28,::FFFF:558E:11C5,6,8,2,1,::FFFF:81C8:CA8B,::FFFF:BADC:CD28,1,0,0,0,::FFFF:81C8:11C5,2,1,0,0,::FFFF:81DC:3111,1,0,1,0 store.bal.... (2 Replies)
Discussion started by: veeruasu
2 Replies

3. Shell Programming and Scripting

Unable to read Environment Variable

Hi I have created the following shell script file with the following content. #!/bin/csh set VAR1="abcxyz" << EOF EOF echo "---------------------" echo "VAR1 = $VAR1" echo "---------------------" i am not able to echo the previously set VAR1. Can any one suggested what could be wrong?... (5 Replies)
Discussion started by: srinu_b
5 Replies

4. Shell Programming and Scripting

read the variable with awk

final.txt file contains SY10020021 SY10023077 3199 4 803.815 11884 4 1825.22 2.2707 say set FIRSTLINE = SY10020021 set SECONDLINE=SY10023077 cat final.txt | awk '{if($1==${FIRSTLINE} & $2==${SECONDLINE}){print $9}else{print "ll"}}'..............this should give me value... (1 Reply)
Discussion started by: Indra2011
1 Replies

5. Shell Programming and Scripting

Expand an environment variable in sed, when the variable contains a slash

I'm trying to make a sed substitution where the substitution pattern is an environment variable to be expanded, but the variable contains a "slash". sed -e 's/<HOME_DIRECTORY>/'$HOME'/'This gives me the following error: sed: -e expression #1, char 21: unknown option to `s'Obviously this is... (2 Replies)
Discussion started by: Ilja
2 Replies

6. Shell Programming and Scripting

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: ... (5 Replies)
Discussion started by: prashas_d
5 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. UNIX for Advanced & Expert Users

How to read pdf file in UNIX environment?

Hi, I had a PDF file in Windows, I ftp'ed it to UNIX environment. Now, I couldnot read the same file in the UNIX environment. Is there any possible way to read this file? I need to give this file as input Thanks, Geetha (4 Replies)
Discussion started by: iamgeethuj
4 Replies

9. Shell Programming and Scripting

use awk to read variable length csv

Any help to read the contents of a variable length csv ....??(using awk) The csv mite look like this : anjali,ram,rahul,mohini,sam,.... and so on ... I need to pick up each name.. Thanks in advance SD (3 Replies)
Discussion started by: shweta_d
3 Replies

10. Shell Programming and Scripting

Read file from within AWK and save $1 to a variable

Hi I am very new to NAWK programming so this question is probably going to sound really stupid: I have a NAWK script which contains a DO loop. During each loop it runs a FORTRAN program which in turn generates two output files , each one containing 2 integer variables. I would appreciate it... (8 Replies)
Discussion started by: robbiegregg
8 Replies
Login or Register to Ask a Question