Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Evaluating environment variables in awk

Posted on 2004-09-30
5
Medium Priority
?
476 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

Question has a verified solution.

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

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
In this post we will learn different types of Android Layout and some basics of an Android App.
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.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

715 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