Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

awk variables

Posted on 2010-08-31
7
Medium Priority
?
473 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
[X]
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
7 Comments
 
LVL 3

Accepted Solution

by:
gremwell earned 1500 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 1500 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

 

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 500 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 1500 total points
ID: 33576421
What is the output of 'svcs -a | grep finger' and  'hostname'  commands?
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

Question has a verified solution.

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

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…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
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 (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

610 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