Solved

awk variables

Posted on 2010-08-31
7
454 Views
Last Modified: 2012-05-10
Hi

 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"

Dan
0
Comment
Question by:d_asselin
  • 3
  • 3
7 Comments
 
LVL 3

Accepted Solution

by:
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.
0
 

Author Comment

by:d_asselin
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"
0
 
LVL 3

Assisted Solution

by:gremwell
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

0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:d_asselin
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"
0
 
LVL 2

Assisted Solution

by:plymelk
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.
0
 

Author Comment

by:d_asselin
ID: 33576285
Hum...
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
Dan
0
 
LVL 3

Assisted Solution

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

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now