Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 478
  • Last Modified:

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
0
neil_mw
Asked:
neil_mw
2 Solutions
 
griesshCommented:
Hi neil_mw, did you try

export VAR=XXX

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

======
Werner
0
 
neil_mwAuthor Commented:
Yes I did, I'm afraid
0
 
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
 
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.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now