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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
|
08-11-2011, 09:32 AM
|
#1
|
|
LQ Newbie
Registered: Mar 2011
Location: Miami
Distribution: Suse, RedHat, Ubuntu, CentOS
Posts: 29
Rep:
|
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!
|
|
|
|
08-11-2011, 10:01 AM
|
#2
|
|
Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 6,328
|
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'
|
|
|
|
08-11-2011, 10:51 AM
|
#3
|
|
LQ Newbie
Registered: Mar 2011
Location: Miami
Distribution: Suse, RedHat, Ubuntu, CentOS
Posts: 29
Original Poster
Rep:
|
Quote:
Originally Posted by grail
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.
|
|
|
|
08-11-2011, 11:32 AM
|
#4
|
|
LQ 5k Club
Registered: Sep 2009
Distribution: Arch x86_64
Posts: 6,443
|
Code:
sed -e 's/.*state:reachable.*/>0/' -e 's/.*state:unreachable.*/<0/'
|
|
|
|
08-11-2011, 11:41 AM
|
#5
|
|
LQ Newbie
Registered: Mar 2011
Location: Miami
Distribution: Suse, RedHat, Ubuntu, CentOS
Posts: 29
Original Poster
Rep:
|
Quote:
Originally Posted by MTK358
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.
|
|
|
|
08-11-2011, 11:50 AM
|
#6
|
|
Senior Member
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 3,943
|
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
|
|
|
|
08-11-2011, 12:03 PM
|
#7
|
|
LQ Newbie
Registered: Mar 2011
Location: Miami
Distribution: Suse, RedHat, Ubuntu, CentOS
Posts: 29
Original Poster
Rep:
|
Quote:
Originally Posted by PTrenholme
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.
|
|
|
|
08-11-2011, 12:26 PM
|
#8
|
|
LQ 5k Club
Registered: Sep 2009
Distribution: Arch x86_64
Posts: 6,443
|
Quote:
Originally Posted by Q_Linux
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.
|
|
|
|
08-11-2011, 12:51 PM
|
#9
|
|
LQ Newbie
Registered: Mar 2011
Location: Miami
Distribution: Suse, RedHat, Ubuntu, CentOS
Posts: 29
Original Poster
Rep:
|
Quote:
Originally Posted by MTK358
Did you try it? It should already do that.
|
Yes I just tried it; my bad., thanks..It works great. Thanks!
|
|
|
|
08-11-2011, 07:39 PM
|
#10
|
|
Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 6,328
|
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'
|
|
|
|
08-12-2011, 09:54 AM
|
#11
|
|
LQ Newbie
Registered: Mar 2011
Location: Miami
Distribution: Suse, RedHat, Ubuntu, CentOS
Posts: 29
Original Poster
Rep:
|
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.
|
|
|
|
08-12-2011, 10:06 AM
|
#12
|
|
LQ 5k Club
Registered: Sep 2009
Distribution: Arch x86_64
Posts: 6,443
|
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?
|
|
|
|
08-12-2011, 10:14 AM
|
#13
|
|
LQ Newbie
Registered: Mar 2011
Location: Miami
Distribution: Suse, RedHat, Ubuntu, CentOS
Posts: 29
Original Poster
Rep:
|
Quote:
Originally Posted by MTK358
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)
|
|
|
|
08-12-2011, 10:31 AM
|
#14
|
|
LQ 5k Club
Registered: Sep 2009
Distribution: Arch x86_64
Posts: 6,443
|
Code:
grep state:unreachable | wc -l
|
|
|
|
08-12-2011, 12:02 PM
|
#15
|
|
LQ Newbie
Registered: Mar 2011
Location: Miami
Distribution: Suse, RedHat, Ubuntu, CentOS
Posts: 29
Original Poster
Rep:
|
Quote:
Originally Posted by MTK358
Code:
grep state:unreachable | wc -l
|
That did seem to work so far. Thanks
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 04:13 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|