This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Linux / UNIX and the fly likes Using variables in awk Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "Using variables in awk" Watch "Using variables in awk" New topic

Using variables in awk

minal silimkar
Ranch Hand

Joined: Nov 25, 2007
Posts: 133
2233|a.k. shukla|g.m.|6000
9876|jai sharma|director|7000
5678|sumit chakrobarty|d.g.m.|10000
2365|barun sengupta|director|7800
1006|chanchal signhvi|director|6700

To get the 2nd field of employee id=5678 I tried below command

$ awk -F "|" '$1 == 5678 {print}' emp.lst | cut -d "|" -f2

I tried same thing, but instead of giving id directly I put that in variable as shown below

$ id=5678
$ awk -F "|" '$1 == $id {print}' emp.lst | cut -d "|" -f2

It is showing nothing

Minal Silimkar
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11404

A nice little catch-22 situation for you.

If you use single quotes (') then the shell variables will not be expanded. So at the point where you have '$1 == $id' the shell has not expanded the shell variable "id", and awk has no idea what "id" is.

Unfortunately if you try to use double quotes (") then the shell will expand the "id" variable, but it will have no clue what the $1 should be expanded to.

One solution is to explicitly pass the variable to awk:

awk -F "|" -v MyID="$id" '$1 == MyID {print}' data | cut -d "|" -f2

Note that you can actually reuse the variable name (although doing so can confuse some people while simultaneously making it easier for others to read) :

awk -F "|" -v id=$id '$1 == id {print}' data | cut -d "|" -f2

An alternative way of doing this would be to make id an environmental variable:

export id=5678

Now you can access it through the ENVIRON array:

awk -F "|" '$1 == ENVIRON["id"] {print}' data | cut -d "|" -f2

Regards, Andrew
[ May 27, 2008: Message edited by: Andrew Monkhouse ]

The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11404

In reading my response, I just realized that I have no idea why you are using cut in your command line. You have already broken each line according to the delimiters, so you might as well use them:

awk -F "|" '$1 == ENVIRON["id"] {print $2}' data

Regards, Andrew
minal silimkar
Ranch Hand

Joined: Nov 25, 2007
Posts: 133
I was not knowing the rule of double quotes and single quotes works same for awk command also.

Thanks Andrew for clearing my concepts regarding quotes and other inforamtion as well.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15960

The double-quote and single-quote rules work for all shell commands as long as you're using one of the main shell programs such as bash, csh or ksh.

The shell "compiles" your command-line text before starting awk. Among other things, this means expanding globs, substituting shell variables (unless escaped or suppressed, as for example in single-quotes) and executing "backtick" commands. So what awk sees is the results of all this, rather than what you actually typed on the command line.

Customer surveys are for companies who didn't pay proper attention to begin with.
I agree. Here's the link:
subject: Using variables in awk
Similar Threads
how to parse a srting using awk command
using sed to chnage data in file
awk script inside a shell script
Select particular field in file in UNIX, Please help me in
php + awk for jsp