Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

newby trying to use "grep | awk" help!

Status
Not open for further replies.

linuxtricks

IS-IT--Management
May 24, 2000
111
0
0
US
hello all

I have a command the shows me the status of two nodes in an active high availability cluster:

[root@box root]# rsfcli -v list

node1:
service stopped auto unblocked
node2:
service running auto unblocked


The output of the 'rsfcli -v list' command above shows that node2 is 'running' the service.

I'd like to be able to display the output of a command on a website to show (at any given moment) which of the two nodes above is 'running'. But, I would like to ONLY display the Node name by itself - along with my own webcode (HTML or php).

i.e.
Currently, "node2" is the active node in the cluster.

I am doing the following to try and achieve this:

[root@box root]# rsfcli -v list | grep -B 1 running | awk '{print $1}'
node2:
service

In the above, I am grepping for the word 'running' and including the line above the result (-B 1) which shows the node name (what I want only), then printing out the results using awk by printing only the column and leaving out the rest of the line that the grep command would normally include.

As you see, awk is extracting the entire 1st column (which exludes the rest of the line that the grep command would normally include - nice) but, it also includes the word 'service'.

Maybe there is another command I should be using in place of awk... but, I'm not sure what that is.

Thanks in advance for your help!


---
Try not. Do. Or do not. There is no try.
 
And what about this ?
rsfcli -v list | grep -B 1 running | awk 'NR=2'

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
PHV,

The command "rsfcli -v list | grep -B 1 running | awk 'NR=2'"
prints out the following:

node2:
service running auto unblocked

Almost as if the last 'awk' part wasn't even there. It's doing just the grep part.

update:
I added another pipe grep to the end.

i.e.
rsfcli -v list | grep -B 1 running | awk '{print $1}'|grep node[12]

and got the result of
node2:


This works! :)
but, so many pipes! I wonder if there is an easier way~

Thanks!

---
Try not. Do. Or do not. There is no try.
 
My bad, sorry for the typo :~/
rsfcli -v list | grep -B 1 running | awk 'NR==2'

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Hi, PHV

I got the same results:

rsfcli -v list | grep -B 1 running | awk 'NR==2'
service running auto unblocked

Thanks

---
Try not. Do. Or do not. There is no try.
 
Shouldn't that be

rsfcli -v list | grep -B 1 running | awk 'NR==1'

or

rsfcli -v list | grep -B 1 running | head -1

CaKiwi
 
Or even

rsfcli -v list | awk '/^node/{a=$0}/running/{print a}'

CaKiwi
 
CaKiwi,

Those commands all worked great! I forgot about the head cmd. I'm not sure what awk 'NR==1' does, though. Nor do I know how do deciper

awk '/^node/{a=$0}/running/{print a}'

Anyhow, thanks very much! Either one of those commands is much cleaner than what I had going. :)

---
Try not. Do. Or do not. There is no try.
 
An awk script consists of a series test-action pairs. If the test is absent it defaults to true. If the action is absent it defaults to print. awk automatically set NR to the current record number, so the test NR==1 returns true for the first line (which is printed since the action defaults to print) and false for any other lines, so the second (or subsequent) line is not printed.

The script '/^node/{a=$0}/running/{print a}' finds any line starting with "node" and saves it in variable a and finds any line containing "running" and prints what was saved in variable a

Hope this helps.





CaKiwi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top