Evaluating environment variables in awk

Hi,

Just a hopefully easy question:

I've got an awk command on a single line namely:

awk -F: '/^[^#]/ {if ($2 == "XXX") printf "%s\n", $1}' /tmp/file.txt

Everything works perfectly, however I need to replace the "XXX" with an environment variable.  Unfortunately, I can't get this to work.  Please help!

Thanks,

Neil
neil_mwAsked:
Who is Participating?
 
geotigerCommented:
Have you tried to pass the OS variable to awk variable using nawk instead awk?

nawk -F: '/^[^#]/ {if ($2 == v1) printf "%s\n", $1}' v1=$XXX /tmp/file.txt


0
 
griesshCommented:
Hi neil_mw, did you try

export VAR=XXX

... ($2==$VAR) ...

======
Werner
0
 
neil_mwAuthor Commented:
Yes I did, I'm afraid
0
 
neil_mwAuthor Commented:
Unfortunately we don't have nawk installed and, as this is a customer machine, we'd have a fight before being able to install it!
0
 
tfewsterCommented:
Which Unix? geotigers comment works in "standard" awk (as supplied on Solaris) as well as in nawk
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.