LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 02-25-2013, 03:56 PM   #1
rahulchandrak
Member
 
Registered: Apr 2010
Posts: 33

Rep: Reputation: 2
capture a word from the output


Hi,

I am trying to catch a value of a variable from a java process from command-line on linux. I search for a process using 'ps -ef' command and get a output like

=======================================================
500 7581 5520 0 00:36 ? 00:00:20 /usr/java/jdk1.6.0_34/jre/bin/java -Xrs -cp ":/foo/barr//bpmengine/bpmengine.jar:/foo/barr//dbconnection/dbconnection.jar:/foo/barr//coboc/coboc.jar:/foo/barr//wsappserver/wsappserver.jar:/foo/barr//scheduler/scheduler.jar:/foo/barr//datamapper/datatransform.jar:/foo/barr//datamapper/binarytransform.jar:/foo/barr//ruleengine/ruleengine.jar:/foo/barr//expressioneval/expressioneval.jar:/foo/barr//audit/auditartifactapi.jar:/foo/barr//audit/auditartifact.jar:/foo/barr//statemachine/statemachine.jar:/opt/abcd/defaultInst/ext/xbean.jar:/foo/barr//bpmengine/dom4j.jar:/foo/barr//bpmengine/geronimo-stax.jar:/foo/barr//bpmengine/poi-ooxml.jar:/foo/barr//bpmengine/poi-ooxml-schemas.jar:/foo/barr//search/poi-3.6-20091214.jar:/foo/barr//bpmengine/bpmengine.jar:/foo/barr//dbconnection/dbconnection.jar:/foo/barr//dbconnectors/dbconnectors.jar:/foo/barr//coboc/coboc.jar:/opt/abcd/defaultInst/ext/xbean.jar:/foo/barr//bpmengine/dom4j.jar:/foo/barr//bpmengine/geronimo-stax.jar:/foo/barr//bpmengine/poi-ooxml.jar:/foo/barr//bpmengine/poi-ooxml-schemas.jar:/foo/barr//search/poi-3.6-20091214.jar:/opt/abcd/defaultInst/abcdcp.jar:/opt/abcd/defaultInst/abcdcp.jar:/usr/lib/mysql-connector-java-5.1.12-bin.jar:/usr/lib/mysql-connector-java-5.1.12-bin.jar::/usr/lib/mysql-connector-java-5.1.21-bin.jar::" -DProcessName=App1 -XX:PermSize=5m -Xmx512M -Dabcd_INSTALL_DIR=/opt/abcd/defaultInst com.eibus.soap.Processor -binary -name 34:115:121:115:116:101:109:35:98:117:115:105:110:101:115:115:95:112:114:111:99:101:115:115:95:109:97 :110:97:103:101:109:101:110:116:35:98:117:115:105:110:101:115:115:95:112:114:111:99:101:115:115:95:1 09:97:110:97:103:101:109:101:110:116:34:
======================================================


In the above output, there is a string called "-DProcessName=App1". the value of the variable "DProcessName" keeps changing. I want to capture the value of the processname only. please notice that the ouput of 'ps -ef' command always is in a single line.

Please help in finding a solution.

Last edited by rahulchandrak; 02-25-2013 at 03:58 PM.
 
Old 02-25-2013, 03:58 PM   #2
schneidz
Senior Member
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 3,917

Rep: Reputation: 600Reputation: 600Reputation: 600Reputation: 600Reputation: 600Reputation: 600
grep -o would probably help in this instance.
 
Old 02-25-2013, 05:47 PM   #3
rahulchandrak
Member
 
Registered: Apr 2010
Posts: 33

Original Poster
Rep: Reputation: 2
Hi,

grep -o option is not helping in this case, for example, If I give, grep -o ProcessName, only ProcessName will be the output, not its value.
 
Old 02-25-2013, 06:35 PM   #4
schneidz
Senior Member
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 3,917

Rep: Reputation: 600Reputation: 600Reputation: 600Reputation: 600Reputation: 600Reputation: 600
grep -o ProcessName.*" "; maybe awk/sed/cut would fit better. you have many options.
 
Old 02-26-2013, 01:11 AM   #5
fortran
Member
 
Registered: Nov 2011
Location: Cairo, Egypt
Distribution: CentOS, RHEL, Fedora
Posts: 300
Blog Entries: 2

Rep: Reputation: 50
In your example, the value of '-DProcessName' is 'App1'.
If you are sure that there will be no space between your values and output of 'ps -ef' is printed in one line like you have put in your post then following command can work for you.
Code:
ps -ef | sed -e 's/.*-DProcessName=//g' -e 's/ .*//g'
The command will not print the text after first space of value of '-DProcessName'

Generally 'ps -ef' command's out put looks like this.
It is appeared in columns.
Code:
UID        PID  PPID  C STIME TTY          TIME CMD
user         1     0  0 10:10 ?        00:00:00 /sbin/init
user         2     0  0 10:10 ?        00:00:00 [kthreadd]
user         3     2  0 10:10 ?        00:00:00 [ksoftirqd/0]
user         4     2  0 10:10 ?        00:00:00 [migration/0]
user         5     2  0 10:10 ?        00:00:00 [watchdog/0]
user         6     2  0 10:10 ?        00:00:00 [migration/1]
user         7     2  0 10:10 ?        00:00:00 [ksoftirqd/1]
user         8     2  0 10:10 ?        00:00:00 [watchdog/1]
user         9     2  0 10:10 ?        00:00:00 [events/0]
user        10     2  0 10:10 ?        00:00:00 [events/1]
user        11     2  0 10:10 ?        00:00:00 [cpuset]
user        12     2  0 10:10 ?        00:00:00 [khelper]
user        13     2  0 10:10 ?        00:00:00 [netns]
user        14     2  0 10:10 ?        00:00:00 [async/mgr]
user        15     2  0 10:10 ?        00:00:00 [pm]
user        17     2  0 10:10 ?        00:00:00 [sync_supers]
user        18     2  0 10:10 ?        00:00:00 [bdi-default]
user        19     2  0 10:10 ?        00:00:00 [kintegrityd/0]
user        20     2  0 10:10 ?        00:00:00 [kintegrityd/1]
If you want to make appeared your value through this output, you can use 'cut' command here.

Last edited by fortran; 02-26-2013 at 01:27 AM.
 
1 members found this post helpful.
Old 02-26-2013, 02:47 AM   #6
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,488

Rep: Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956
More concisely:
Code:
ps -ef | sed -r 's/.*-DProcessName=([^ ]+).*/\1/'
 
1 members found this post helpful.
Old 02-26-2013, 06:04 AM   #7
rahulchandrak
Member
 
Registered: Apr 2010
Posts: 33

Original Poster
Rep: Reputation: 2
Thank you

Hi All,

Thank you all for the help. I am very bad at working with regular expressions. I will start working on it.
 
  


Reply


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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to capture an output? ethanlokesh Linux - General 3 09-20-2012 10:14 AM
looking for key word on the output Madison00 Programming 2 10-18-2010 05:07 PM
How to output first character of a word in capital? Hi_This_is_Dev Linux - Server 5 09-28-2009 09:44 PM
grepping last word of output rajatgarg Programming 3 11-25-2003 10:41 AM


All times are GMT -5. The time now is 11:03 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