Looking for a better way to extract a string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Looking for a better way to extract a string
# 1  
Old 06-09-2009
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..
# 2  
Old 06-09-2009
Code:
ps aux | nawk '/java/ && match($0,/-Dserver.root=[^ ]*/) {split(substr($0,RSTART,RLENGTH),a,"="); print a[2]}'
OR
ps aux | nawk '/java/ && match($0,/-Dserver.root=[^ ]*/) {a=substr($0,RSTART,RLENGTH); print substr(a,index(a,"=")+1) }'

# 3  
Old 06-09-2009
A sed version:

Code:
ps aux | sed -n '/java/ s/.*Dserver.root=\([^ ]*\) .*/\1/p'

# 4  
Old 06-09-2009
For some reason, both awk methods and the sed method all return a little something extra:

Code:
[me]$ ps aux | sed -n '/java/ s/.*Dserver.root=\([^ ]*\) .*/\1/p'
\([^
/opt/resin-pro-3.0.21-inst01
/opt/resin-pro-3.0.21-inst02

[me]$ ps aux | awk '/java/ && match($0,/-Dserver.root=[^ ]*/) {split(substr($0,RSTART,RLENGTH),a,"="); print a[2]}'
[^
/opt/resin-pro-3.0.21-inst01
/opt/resin-pro-3.0.21-inst02

[me]$ ps aux | awk '/java/ && match($0,/-Dserver.root=[^ ]*/) {a=substr($0,RSTART,RLENGTH); print substr(a,index(a,"=")+1) }'
[^
/opt/resin-pro-3.0.21-inst01
/opt/resin-pro-3.0.21-inst02

# 5  
Old 06-09-2009
Strange. Given a quote sample of data, my solution returns:
Code:
/opt/resin-pro-3.0.21-inst01
/opt/resin-pro-3.0.21-inst02


Last edited by vgersh99; 06-09-2009 at 06:00 PM..
# 6  
Old 06-09-2009
Strange...if the order of the parameters are constant you can try this:
Code:
ps aux | sed -n '/java/ s/.*Dserver.root=\(.*\) -Djava.u.*/\1/p'

# 7  
Old 06-09-2009
Quote:
Originally Posted by Franklin52
Strange...if the order of the parameters are constant you can try this:
Code:
ps aux | sed -n '/java/ s/.*Dserver.root=\(.*\) -Djava.u.*/\1/p'

The order of parameters is currently constant but I don't want to rely on that. This newest version of the sed returns a little more than the last:

Code:
[me]$ ps aux | sed -n '/java/ s/.*Dserver.root=\(.*\) -Djava.u.*/\1/p'
\(.*\)
/opt/resin-pro-3.0.21-inst01
/opt/resin-pro-3.0.21-inst02

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to extract every repeated string between two specific string?

Hello guys, I have problem with hpux shell script. I have one big text file that contains like SOH bla bla bla bla bla bla ETX SOH bla bla bla ETX SOH bla bla bla ETX What I need to do is save first SOH*BLA into file1.txt, save second SOH*BLA into file2.txt and so on.... (17 Replies)
Discussion started by: sembii
17 Replies

2. Shell Programming and Scripting

To Search for a string and to extract the string from the text

Hi Team I have an huge xml where i need to search for a ceratin numbers. For example 2014-05-06 15:15:41,498 INFO WebContainer : 10 CommonServicesLogs - CleansingTriggerService.invokeCleansingService Entered PUBSUB NOTIFY MESSAGE () - <?xml version="1.0" encoding="UTF-8"... (5 Replies)
Discussion started by: Kannannair
5 Replies

3. Shell Programming and Scripting

Search String and extract few lines under the searched string

Need Assistance in shell programming... I have a huge file which has multiple stations and i wanted to search particular station and extract few lines from it and the rest is not needed Bold letters are the stations . The whole file has multiple stations . Below example i wanted to search... (4 Replies)
Discussion started by: ajayram_arya
4 Replies

4. Shell Programming and Scripting

Extract a string from another string in UNIX

I have a string string="Please have a nice day and sleep well Replace_12123_31233_32134_12342 Good day" How do i replace "Replace_12123_31233_32134_1234" in the above string.?? Please help. Regards, Qwerty (3 Replies)
Discussion started by: qwertyu
3 Replies

5. Shell Programming and Scripting

Extract a string between 2 ref string from a file

Hi, May i ask if someone share some command for extracting a string between 2 ref string in a txt file My objective: i had a file with multiple lines and wants only to extract the string "watch?v=IbkAXOmEHpY" or "watch?v=<11 random character>", when i used "grep 'watch?=*' i got a results per... (4 Replies)
Discussion started by: jao_madn
4 Replies

6. Shell Programming and Scripting

to extract string from main string and string comparison

continuing from my previous post, whose link is given below as a reference https://www.unix.com/shell-programming-scripting/171076-shell-scripting.html#post302573569 consider there is create table commands in a file for eg: CREATE TABLE `Blahblahblah` ( `id` int(11) NOT NULL... (2 Replies)
Discussion started by: vivek d r
2 Replies

7. Shell Programming and Scripting

extract a string within a string using a pattern

hi all, i have a file name using the following pattern: PREFIX: AR SOURCE: LEGACY DATETIME: YYYYMMDD_HH24MISS SUFFIX: .txt sample filename: AR_LEGACY_20101104_105500.txt i want to extract the source which is LEGACY in this case. how do i do this using shell? thanks. (4 Replies)
Discussion started by: adshocker
4 Replies

8. Shell Programming and Scripting

Search for string in a file and extract another string to a variable

Hi, guys. I have one question: I need to search for a string in a file, and then extract another string from the file and assign it to a variable. For example: the contents of the file (group) is below: ... ftp:x:23: mail:x:34 ... testing:x:2001 sales:x:2002 development:x:2003 ...... (6 Replies)
Discussion started by: daikeyang
6 Replies

9. Shell Programming and Scripting

extract a sub string from a main string

i need a shell program to extract a substring from a main string.. for eg:- main string is madhu.. sub string is mad o/p:- be mad. try to solve this one (5 Replies)
Discussion started by: madhu.it
5 Replies

10. UNIX for Dummies Questions & Answers

How to extract a portion of a string from the whole string

How to extract a portion of a string from a full string using unix. For example: Say source string is = "req92374923.log" I want only the numeric portion of the string say "92374923" how to do that in Unix. (2 Replies)
Discussion started by: ds_sastry
2 Replies
Login or Register to Ask a Question