LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   capture a word from the output (https://www.linuxquestions.org/questions/linux-general-1/capture-a-word-from-the-output-4175451665/)

rahulchandrak 02-25-2013 03:56 PM

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.

schneidz 02-25-2013 03:58 PM

grep -o would probably help in this instance.

rahulchandrak 02-25-2013 05:47 PM

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.

schneidz 02-25-2013 06:35 PM

grep -o ProcessName.*" "; maybe awk/sed/cut would fit better. you have many options.

fortran 02-26-2013 01:11 AM

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.

colucix 02-26-2013 02:47 AM

More concisely:
Code:

ps -ef | sed -r 's/.*-DProcessName=([^ ]+).*/\1/'

rahulchandrak 02-26-2013 06:04 AM

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.


All times are GMT -5. The time now is 05:30 PM.