• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Using variables in awk

 
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
2233|a.k. shukla|g.m.|6000
9876|jai sharma|director|7000
5678|sumit chakrobarty|d.g.m.|10000
2365|barun sengupta|director|7800
5423|n.k.gupta|chairman|5400
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
 
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Andrew Monkhouse
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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-Urankar
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Saloon Keeper
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic