Grep for a specific string

luser9999
luser9999 used Ask the Experts™
on
Hi,

I have many tomcat instances are running on Linux server and I would like to grep them based on a string called "catalina.base" here is one example


root     27180     1  0 Feb25 ?        00:02:10 /opt/java/jdk1.5.0_16//bin/java -Djava.util.logging.config.file=/opt/net/crowd-2.0.3/apache-tomcat/conf/logging.properties -Xms128m -Xmx256m -XX:MaxPermSize=256m -Dfile.encoding=UTF-8 -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.endorsed.dirs=/opt/net/crowd-2.0.3/apache-tomcat/endorsed -classpath :/opt/net/crowd-2.0.3/apache-tomcat/bin/bootstrap.jar -Dcatalina.base=/opt/net/crowd-2.0.3/apache-tomcat -Dcatalina.home=/opt/net/crowd-2.0.3/apache-tomcat -Djava.io.tmpdir=/opt/net/crowd-2.0.3/apache-tomcat/temp org.apache.catalina.startup.Bootstrap start

So when i  " ps -ef | grep java"  --> will return all the instances of java and out of which I need only the string value "-Dcatalina.base="

to make it clear my search out put should be,

-Dcatalina.base=/opt/net/crowd-2.0.3/apache-tomcat
-Dcatalina.base=/opt/test/tomcat-2.0.3/apache-tomcat
-Dcatalina.base=/opt/test123/staging/apache-tomcat
-Dcatalina.base=/home/catalina/catalina-2.0/apache-tomcat

thanks

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
ps -ef | grep java" | sed 's/.*\(-Dcatalina.base=.*\)/\1/'
Most Valuable Expert 2013
Top Expert 2013

Commented:
ps -ef | awk '{FS="-Dcatalina.base" ; /catalina.base/ ;print FS $2}' | cut -f1 -d" "
ps -ef | grep java" | sed 's/.*\(-Dcatalina.base=.*apache-tomcat\).*/\1/'
Success in ‘20 With a Profitable Pricing Strategy

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Commented:
ps -ef | grep -o "-Dcatalina.base=/opt/net/crowd-2.0.3/apache-tomcat"

This should work.

Commented:
Sorry for above comment, It could work on a file only.
ps -ef | grep "java" | sed 's/.*\(-Dcatalina.base=.*apache-tomcat\).*/\1/'

Uneven quotes in last post

Author

Commented:
Sorry guys, executed and none of these are working except for woolmilkporc: where I get only three matches and reaming  are


-Dcatalina.base
-Dcatalina.base
-Dcatalina.base
-Dcatalina.base
-Dcatalina.base
-Dcatalina.base
-Dcatalina.base
-Dcatalina.base
-Dcatalina.base
-Dcatalina.base
-Dcatalina.base
-Dcatalina.base
-Dcatalina.base
-Dcatalina.base
-Dcatalina.base
-Dcatalina.base
-Dcatalina.base
-Dcatalina.base
-Dcatalina.base
-Dcatalina.base
-Dcatalina.base
-Dcatalina.base
 like many... I actually have 22 instances are running.

thanks for helping
Most Valuable Expert 2013
Top Expert 2013

Commented:
"grep -o is not quite bad:

ps -ef grep -o -e "-Dcatalina\.base.*" | cut -f1 -d" " 

Author

Commented:
sorry, I got all 22 after using  woolmilkporc: command but remaining are
-Dcatalina.base
-Dcatalina.base
-Dcatalina.base
-Dcatalina.base
-Dcatalina.base
-Dcatalina.base
-Dcatalina.base
-Dcatalina.base
-Dcatalina.base
-Dcatalina.base
-Dcatalina.base
-Dcatalina.base
-Dcatalina.base
-Dcatalina.base
-Dcatalina.base
-Dcatalina.base
-Dcatalina.base

can we remove these lines?
thanks
Most Valuable Expert 2013
Top Expert 2013

Commented:
Yes, that's right.

Try this:

ps -ef | grep "catalina.base" | awk '{FS="-Dcatalina.base" ; print FS $2}' | cut -f1 -d" " 
Most Valuable Expert 2013
Top Expert 2013
Commented:
... or the "grep -o" thing, but with a pipe this time:

ps -ef |grep -o -e "-Dcatalina\.base.*" | cut -f1 -d" " 

Author

Commented:
thanks

ps -o -e worked like charm

thanks man

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial