LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Help with a simple shell script (http://www.linuxquestions.org/questions/programming-9/help-with-a-simple-shell-script-896763/)

Q_Linux 08-11-2011 09:32 AM

Help with a simple shell script
 
I am relatively new to scripting and was wondering if someone could help me with my dilemma. I use IBM WebSphere and the command I use is:

xsadmin.sh -p 11111 -bp 11111 -ch servername123 -routetable

The output is an example of this:

P: partition:83 - state:reachable - ipaddress:servername100 - zone:ZoneB
> R: partition:83 - state:reachable - ipaddress:servername101- zone:ZoneA
P: partition:84 - state:reachable - ipaddress:servername102 - zone:ZoneB
> R: partition:84 - state:reachable - ipaddress:servername103 - zone:ZoneA
P: partition:85 - state:reachable - ipaddress:servername104 - zone:ZoneA
> R: partition:85 - state:reachable - ipaddress:servername105 - zone:ZoneB

And so on.

I need to condense the output of this file to send to SiteScope as a file with only values of either <0 or >0. Meaning, if the state:reachable it needs to appear as >0. if state:unreachable than the value needs to appear as <0. I basically need to find a way to remove all erroneous info and substitute the words state:reachable or state:unreachable with either <0 or >0. I have tried many different methods, awk, sed and grep but seem to come up short. Any help would be most appreciative and would be a great learning tool.

Thanks!

grail 08-11-2011 10:01 AM

Not sure I understand ... is this what your looking for?
Code:

xsadmin.sh -p 11111 -bp 11111 -ch servername123 -routetable | awk 'BEGIN{OFS=FS="-"}{if($2 ~ /state:reachable/)$2 = "<0";if($2 ~ /state:unreachable/)$2 = ">0"}1'

Q_Linux 08-11-2011 10:51 AM

Quote:

Originally Posted by grail (Post 4439689)
Not sure I understand ... is this what your looking for?
Code:

xsadmin.sh -p 11111 -bp 11111 -ch servername123 -routetable | awk 'BEGIN{OFS=FS="-"}{if($2 ~ /state:reachable/)$2 = "<0";if($2 ~ /state:unreachable/)$2 = ">0"}1'

Thanks! I think we are almost there. The only think I did to the script was change the values the other way around. 'BEGIN{OFS=FS="-"}{if($2 ~ /state:reachable/)$2 = ">0";if($2 ~ /state:unreachable/)$2 = "<0"}1'

So now the output looks like which is correct given the >0 means state:reachable.

P: partition:197 ->0- ipaddress:servername101 - zone:ZoneA
> R: partition:197 ->0- ipaddress:servername101 - zone:ZoneB
P: partition:198 ->0- ipaddress:servername102 - zone:ZoneA
> R: partition:198 ->0- ipaddress:servername102 - zone:ZoneB
P: partition:199 ->0- ipaddress:servername103 - zone:ZoneA
> R: partition:199 ->0- ipaddress:servername103 - zone:ZoneB

How do I remove the rest of the output to make the data just look like this?

>0
>0
>0
>0
>0

MTK358 08-11-2011 11:32 AM

Code:

sed -e 's/.*state:reachable.*/>0/' -e 's/.*state:unreachable.*/<0/'

Q_Linux 08-11-2011 11:41 AM

Quote:

Originally Posted by MTK358 (Post 4439819)
Code:

sed -e 's/.*state:reachable.*/>0/' -e 's/.*state:unreachable.*/<0/'

Thanks for the tip. What do you suggest I do to remove all the information from the output, for instance; I don't need all this output.

P: partition:197 - ipaddress:servername101 - zone:ZoneA
> R: partition:197 - ipaddress:servername101 - zone:ZoneB
P: partition:198 - ipaddress:servername102 - zone:ZoneA
> R: partition:198 - ipaddress:servername102 - zone:ZoneB

I just need for the data to look like:

>0
>0
>0

or

<0
<0
<0

I have to have it in a special format to upload to SiteScope.

PTrenholme 08-11-2011 11:50 AM

Try this one:
Code:

xsadmin.sh -p 11111 -bp 11111 -ch servername123 -routetable | awk '/state:reachable/{print "<0";}/state:unreachable/{print ">0";}'
Note: Untested code

Q_Linux 08-11-2011 12:03 PM

Quote:

Originally Posted by PTrenholme (Post 4439850)
Try this one:
Code:

xsadmin.sh -p 11111 -bp 11111 -ch servername123 -routetable | awk '/state:reachable/{print "<0";}/state:unreachable/{print ">0";}'
Note: Untested code

Thanks, that does appear to work. Now my challenge is to remove all data and just have the output appear as:

>0
>0
>0

Right now the data appears as:

P: partition:197 ->0- ipaddress:servername101 - zone:ZoneA
> R: partition:197 ->0- ipaddress:servername101 - zone:ZoneB
P: partition:198 ->0- ipaddress:servername102 - zone:ZoneA
> R: partition:198 ->0- ipaddress:servername102 - zone:ZoneB
P: partition:199 ->0- ipaddress:servername103 - zone:ZoneA
> R: partition:199 ->0- ipaddress:servername103 - zone:ZoneB

I don't need the other data. I have tried some sed commands to remove, but I don't know how to remove the random numbers associated with the output, for example the numbers which appear after partition:199 or server names with numbers, for example nc2sz2ecmas19 or nc3sz2ecmas10.

MTK358 08-11-2011 12:26 PM

Quote:

Originally Posted by Q_Linux (Post 4439835)
Thanks for the tip. What do you suggest I do to remove all the information from the output, for instance; I don't need all this output.

P: partition:197 - ipaddress:servername101 - zone:ZoneA
> R: partition:197 - ipaddress:servername101 - zone:ZoneB
P: partition:198 - ipaddress:servername102 - zone:ZoneA
> R: partition:198 - ipaddress:servername102 - zone:ZoneB

Did you try it? It should already do that.

Q_Linux 08-11-2011 12:51 PM

Quote:

Originally Posted by MTK358 (Post 4439899)
Did you try it? It should already do that.

Yes I just tried it; my bad., thanks..It works great. Thanks!

grail 08-11-2011 07:39 PM

Actually, if one or the other word, (un)reachable, is always on each line:
Code:

xsadmin.sh -p 11111 -bp 11111 -ch servername123 -routetable | awk '$0 = (/unreachable/?"<":">")0'

Q_Linux 08-12-2011 09:54 AM

Needs a little tweaking.
 
Thanks for your help so far on this, I received further clarification as
to what I need to do and I think it requires a little tweaking. For the
ones that are "state:reachable" I don't need any value returned, or
maybe a simple echo statement that says "reachable" or a numeric value
of "-1". I can change it if necessary.. For the ones that are in
"state:unreachable". I need a count of how many return like that. Say
5,6,7, or so on. So the output I am interested in is:

-1 (good)

or

5 (bad)

This is the script so far that I have. For a reference point.

#!/bin/sh

export JAVA_HOME=/hosting/products/WebSphereU01/java

#sets the environment in java

#the values -p -bp and -ch will need to be altered according to what
needs to be run.

/hosting/ogsa/ogsa-6.1.0.5/ObjectGrid/bin/xsadmin.sh -p 11111 -bp 11111
-ch server0123 -routetable | sed -e 's/.*state:reachable.*/>0/' e
's/.*state:unreachable.*/<0/' > `date "+%y%m%d%H%M%S"`.txt

This is the output it produces in its current form. It counts the number of hosts on the grid. I don't need this many as an output, Just the ones from above in that format.

>0

>0

>0

And so forth.

MTK358 08-12-2011 10:06 AM

I don't understand, so you want it to print the number of "unreachable" lines if there are any, and print "-1" if there aren't any?

Do you still want the ">0" and "<0" written the the text file, like in your script in the above post?

Q_Linux 08-12-2011 10:14 AM

Quote:

Originally Posted by MTK358 (Post 4440932)
I don't understand, so you want it to print the number of "unreachable" lines if there are any, and print "-1" if there aren't any?

Do you still want the ">0" and "<0" written the the text file, like in your script in the above post?

Yeah sorry, I received more specific information and basically the values need to be changed. Sitescope won't recognize the <0 or >0 repeated many lines over. I just need one value to indicate all are reachable and one numeric value to indicate how many are unreachable, say 5 or so. So out put would be say:

0 (for reachable)

5 (for unreachable)

MTK358 08-12-2011 10:31 AM

Code:

grep state:unreachable | wc -l

Q_Linux 08-12-2011 12:02 PM

Quote:

Originally Posted by MTK358 (Post 4440960)
Code:

grep state:unreachable | wc -l

That did seem to work so far. Thanks


All times are GMT -5. The time now is 10:39 AM.