LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 08-11-2011, 09:32 AM   #1
Q_Linux
LQ Newbie
 
Registered: Mar 2011
Location: Miami
Distribution: Suse, RedHat, Ubuntu, CentOS
Posts: 29

Rep: Reputation: 0
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!
 
Old 08-11-2011, 10:01 AM   #2
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,476

Rep: Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888
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'
 
Old 08-11-2011, 10:51 AM   #3
Q_Linux
LQ Newbie
 
Registered: Mar 2011
Location: Miami
Distribution: Suse, RedHat, Ubuntu, CentOS
Posts: 29

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by grail View Post
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

Last edited by Q_Linux; 08-11-2011 at 11:37 AM.
 
Old 08-11-2011, 11:32 AM   #4
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
Code:
sed -e 's/.*state:reachable.*/>0/' -e 's/.*state:unreachable.*/<0/'
 
Old 08-11-2011, 11:41 AM   #5
Q_Linux
LQ Newbie
 
Registered: Mar 2011
Location: Miami
Distribution: Suse, RedHat, Ubuntu, CentOS
Posts: 29

Original Poster
Rep: Reputation: 0
Smile

Quote:
Originally Posted by MTK358 View Post
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.
 
Old 08-11-2011, 11:50 AM   #6
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,149

Rep: Reputation: 330Reputation: 330Reputation: 330Reputation: 330
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
 
Old 08-11-2011, 12:03 PM   #7
Q_Linux
LQ Newbie
 
Registered: Mar 2011
Location: Miami
Distribution: Suse, RedHat, Ubuntu, CentOS
Posts: 29

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by PTrenholme View Post
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.
 
Old 08-11-2011, 12:26 PM   #8
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
Quote:
Originally Posted by Q_Linux View Post
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.
 
Old 08-11-2011, 12:51 PM   #9
Q_Linux
LQ Newbie
 
Registered: Mar 2011
Location: Miami
Distribution: Suse, RedHat, Ubuntu, CentOS
Posts: 29

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by MTK358 View Post
Did you try it? It should already do that.
Yes I just tried it; my bad., thanks..It works great. Thanks!
 
Old 08-11-2011, 07:39 PM   #10
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,476

Rep: Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888
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'
 
Old 08-12-2011, 09:54 AM   #11
Q_Linux
LQ Newbie
 
Registered: Mar 2011
Location: Miami
Distribution: Suse, RedHat, Ubuntu, CentOS
Posts: 29

Original Poster
Rep: Reputation: 0
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.
 
Old 08-12-2011, 10:06 AM   #12
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
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?
 
Old 08-12-2011, 10:14 AM   #13
Q_Linux
LQ Newbie
 
Registered: Mar 2011
Location: Miami
Distribution: Suse, RedHat, Ubuntu, CentOS
Posts: 29

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by MTK358 View Post
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)
 
Old 08-12-2011, 10:31 AM   #14
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
Code:
grep state:unreachable | wc -l
 
Old 08-12-2011, 12:02 PM   #15
Q_Linux
LQ Newbie
 
Registered: Mar 2011
Location: Miami
Distribution: Suse, RedHat, Ubuntu, CentOS
Posts: 29

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by MTK358 View Post
Code:
grep state:unreachable | wc -l
That did seem to work so far. Thanks
 
  


Reply

Tags
awk, script, sed, shell, websphere


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Simple shell script vinaytp Linux - Newbie 4 03-30-2011 03:32 AM
please help with simple shell script blancs Programming 6 11-02-2008 04:10 AM
need shell-script help (very simple) oskar Linux - General 6 03-12-2008 07:08 AM
Need help getting started simple simple shell script dhonnoll78 Programming 6 12-17-2007 05:34 PM
Simple C Shell script is not so simple elconde Programming 2 09-16-2001 11:53 PM


All times are GMT -5. The time now is 04:59 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration