Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 478
  • Last Modified:

awk variables

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
d_asselin
Asked:
d_asselin
  • 3
  • 3
4 Solutions
 
gremwellCommented:
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
 
d_asselinAuthor Commented:
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
 
gremwellCommented:
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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
d_asselinAuthor Commented:
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
 
plymelkCommented:
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
 
d_asselinAuthor Commented:
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
 
gremwellCommented:
What is the output of 'svcs -a | grep finger' and  'hostname'  commands?
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now