Solved

awk variables

Posted on 2010-08-31
7
455 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
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…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

911 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

24 Experts available now in Live!

Get 1:1 Help Now