Evaluating environment variables in awk

Posted on 2004-09-30
Last Modified: 2013-12-26

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!


Question by:neil_mw
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
LVL 11

Expert Comment

ID: 12190712
Hi neil_mw, did you try

export VAR=XXX

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


Author Comment

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

Accepted Solution

geotiger earned 50 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


Author Comment

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!
LVL 21

Assisted Solution

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

Featured Post

Get MongoDB database support online, now!

At Percona’s web store you can order your MongoDB database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card. Handle your MongoDB database support now!

Question has a verified solution.

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

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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 this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

615 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