Solved

Evaluating environment variables in awk

Posted on 2004-09-30
5
473 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 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


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 20

Assisted Solution

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

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
method notes when mouse over in eclipse 5 87
Modbus - whats the maximum I can store in one register? 4 80
noX challenge 17 129
Excel file not created as expected 7 79
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
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.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

838 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