|
Looking for a better way to extract a string
This is a question about optimization. I can do what I want but the users of this forum always find a way to do it better and I end up learning from it.
We have resin application servers. I want to extract a variable from the running processes:
Code:
[me]$ ps aux | grep java
resin 15212 19.8 10.9 3220756 1797684 ? Sl Jun04 1554:10 /usr/java/default/bin/java -Xms2304m -Xmx2304m -XX:NewRatio=8 -XX:+HeapDumpOnOutOfMemoryError -XX:PermSize=128m -XX:MaxPermSize=128m -DjiveHome=/opt/jiveHome-inst01 -Dhostname=blah -verbose:gc -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+PrintTenuringDistribution -Dtangosol.coherence.machine=blah -Dtangosol.coherence.member=inst01 -Dtangosol.coherence.localhost=10.17.21.167 -Dtangosol.coherence.localport=16000 -Dtangosol.coherence.clusterport=24211 -Dtangosol.coherence.clusteraddress=224.2.4.0 -Dtangosol.coherence.log.level=9 -Dcom.sun.jndi.ldap.connect.pool.timeout=120000 -Dcom.sun.management.jmxremote.port=30001 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.ssl=false -Doracle.net.tns_admin=/opt/oracle/network/admin -Dc3p0.maxIdleTime=600 -Dnntp.ip=10.17.21.167 -Dnntp.listen.port=8089 -Xss128k -Dresin.home=/opt/resin-pro-3.0.21-inst01 -Dserver.root=/opt/resin-pro-3.0.21-inst01 -Djava.util.logging.manager=com.caucho.log.LogManagerImpl -Djavax.management.builder.initial=com.caucho.jmx.MBeanServerBuilderImpl com.caucho.server.resin.Resin -socketwait 53858
resin 15214 19.7 11.8 3217244 1945076 ? Sl Jun04 1541:40 /usr/java/default/bin/java -Xms2304m -Xmx2304m -XX:NewRatio=8 -XX:+HeapDumpOnOutOfMemoryError -XX:PermSize=128m -XX:MaxPermSize=128m -DjiveHome=/opt/jiveHome-inst02 -Dhostname=blah -verbose:gc -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+PrintTenuringDistribution -Dtangosol.coherence.machine=blah -Dtangosol.coherence.member=inst01 -Dtangosol.coherence.localhost=10.17.21.168 -Dtangosol.coherence.localport=16000 -Dtangosol.coherence.clusterport=24211 -Dtangosol.coherence.clusteraddress=224.2.4.0 -Dtangosol.coherence.log.level=9 -Dcom.sun.jndi.ldap.connect.pool.timeout=120000 -Dcom.sun.management.jmxremote.port=30002 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.ssl=false -Doracle.net.tns_admin=/opt/oracle/network/admin -Dc3p0.maxIdleTime=600 -Dnntp.ip=10.17.21.168 -Dnntp.listen.port=8089 -Xss128k -Dresin.home=/opt/resin-pro-3.0.21-inst02 -Dserver.root=/opt/resin-pro-3.0.21-inst02 -Djava.util.logging.manager=com.caucho.log.LogManagerImpl -Djavax.management.builder.initial=com.caucho.jmx.MBeanServerBuilderImpl com.caucho.server.resin.Resin -socketwait 33796
17814 26011 0.0 0.0 61140 712 pts/3 S+ 10:47 0:00 grep java
I'm looking to extract the bold part of that, Dserver.root. It won't always be the same info after Dserver.root= but I will always need all the information between the equal sign and the trailing space.
Here's how I did it:
Code:
[me]$ ps aux | grep java | tr ' ' '\n' | grep Dserver.root | awk -F= '{ print $2 }'
/opt/resin-pro-3.0.21-inst01
/opt/resin-pro-3.0.21-inst02
That works but I'm sure I'm piping too much or some whiz can come along and say "I can name that string in 10 characters". I learn a lot that way. Or, maybe, you can just say "WOW. What an awesome solution to that problem. You are truly one of the greatest this forum has ever seen. Do you sell t-shirts with your face on them?"
(EDIT) Do I need to add my own line breaks to code blocks to keep them from extending out like these did?(/EDIT)
Thanks,
Mike G.
Last edited by mglenney; 06-09-2009 at 03:17 PM..
|