awk variables

Posted on 2010-08-31
Last Modified: 2012-05-10

 I’m thring to pass a variable to awk or nawk with no success

I need to find the proper syntax to do this one liner. A bit of
Help would be nice¿

svcs -a | grep finger |  nawk -v HN='hostname' '{print "\$HN\""$1"\",\" " "yes" "\",\" " $3"\" "}'
 which produces
$HN"online"," yes"," svc:/network/finger:default"

But what I need to produce is this
 “servername”,”  online"," yes"," svc:/network/finger:default"

And even the “yes” is not ok it should be a eval  if the second field = online it should be No
                                     And correspondingly the field is disable the field should be no
Like the examples below
“servername”,”  online"," No"," svc:/network/finger:default"

“servername”,”disable ","yes"," svc:/network/finger:default"

Question by:d_asselin
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3

Accepted Solution

gremwell earned 375 total points
ID: 33569333
awk -v HN="`hostname`" '{print "\"" HN "\",", "\""$1"\"", "\"yes\", \""$3"\""}'

I assume you have meant to assign the output of 'hostname' command to HN variable. In this case you should use backticks around 'hostname' command instead of single quotes, as shown in the example above.

Author Comment

ID: 33569375
Thank you
  but no kandy I tried that
-bash-3.00# svcs -a | grep finger |  nawk -v HN=`hostname` '{print "\$HN\""$1"\",\" " "yes" "\",\" " $3"\" "}'
with this result

$HN"online"," yes"," svc:/network/finger:default"

Assisted Solution

gremwell earned 375 total points
ID: 33569523
I have an impression you have tried the initial version of AWK script, not the one I have suggested.

I don't have 'svcs' command on my Ubuntu, but I get the following:

abb@d820:~$ echo a b c | nawk -v HN="`hostname`" '{print "\"" HN "\",", "\""$1"\"", "\"yes\", \""$3"\""}'

"d820", "a" "yes", "c"

Open in new window

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.


Author Comment

ID: 33569559
Yup that works fine
 now! how do you do a eval ?
 if the second field = online the 3 field should be a NO
 and if the second field is disable then I should be a yes

svcs -a | grep finger | nawk -v HN="`hostname`" '{print "\"" HN "\",", "\""$1"\"", "\"yes\", \""$3"\""}'
"cdpqda", "online" "yes", "svc:/network/finger:default"

Assisted Solution

plymelk earned 125 total points
ID: 33573715
Here is a literal (but simple) example for your request for an 'eval'
nawk 'if ($2 == "online ) {print  "NO"} else {if ($2 == "disable") {print "yes"}}'
Below is an example that I believe is more along what you wanted.
[plymelk@web-svr ~]$ echo online one svc:/network/finger:default |  awk -v HN=`hostname -s` '{if ( $2 == "online" ) {print "\""HN"\", \""$1"\", \"" "yes" "\", \""$3"\" "} else {print "\""HN"\", \""$1"\", \"no\", \""$3"\" "} }'
"web-svr", "disabled", "no", "svc:/network/finger:default"

[plymelk@web-svr ~]$ echo online online svc:/network/finger:default |  awk -v HN=`hostname -s` '{if ( $2 == "online" ) {print "\""HN"\", \""$1"\", \"" "yes" "\", \""$3"\" "} else {print "\""HN"\", \""$1"\", \"no\", \""$3"\" "} }'
"web-svr", "online", "yes", "svc:/network/finger:default"
[plymelk@web-svr ~]$
Good Luck.

Author Comment

ID: 33576285
Strange stuff…

 The output I’m getting is not correct but I can’t find the error
svcs -a | grep finger |  nawk -v HN=`hostname` '{if ( $2 == "online" ) {print "\""HN"\", \""$1"\", \"" "yes" "\", \""$3"\" "} else {print "\""HN"\", \""$1"\", \"no\", \""$3"\" "} }'

"-s", "online", "no", "svc:/network/finger:default"  

I don’t know where that  -s is coming from.  I removed it from the line because this
Is a Solaris 10 box and hostname  -s is not a supported option in Solaris  I should
have the server name in lieux of -s

Assisted Solution

gremwell earned 375 total points
ID: 33576421
What is the output of 'svcs -a | grep finger' and  'hostname'  commands?

Featured Post

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

Recently, an awarded photographer, Selina De Maeyer (, completed a photo shoot of a beautiful event ( in An…
Utilizing an array to gracefully append to a list of EmailAddresses
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a recent question ( here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

691 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