Thursday, July 9, 2009

How to use an external variable inside awk command?

Some times we need to pass external shell variables inside awk statement. Here is a way to do it

suppose I want to use $user in my awk command
$ export user=uid #$user is defined

Pass it to awk command with -v option

$ cat /etc/passwd|tail -1|awk -v var=$user -F":" '{print "Your " var " is " $1}'

o/p: Your uid is guruss1