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
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 20

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need a Shell script to start a service checking the port 6 50
sumNumber challenge 16 122
Doc'in system (example?) BA 7 109
Not needed 13 111
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.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

776 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