Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Evaluating environment variables in awk

Posted on 2004-09-30
5
Medium Priority
?
477 Views
Last Modified: 2013-12-26
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
Comment
Question by:neil_mw
5 Comments
 
LVL 11

Expert Comment

by:griessh
ID: 12190712
Hi neil_mw, did you try

export VAR=XXX

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

======
Werner
0
 

Author Comment

by:neil_mw
ID: 12191028
Yes I did, I'm afraid
0
 
LVL 12

Accepted Solution

by:
geotiger earned 200 total points
ID: 12191221
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
 

Author Comment

by:neil_mw
ID: 12191263
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
 
LVL 21

Assisted Solution

by:tfewster
tfewster earned 200 total points
ID: 12191892
Which Unix? geotigers comment works in "standard" awk (as supplied on Solaris) as well as in nawk
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

879 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question