Extract string from output.

I need to extract the the values of "Java"and "-Dplatform.home" from the output of the below ps command.

ps -xef | grep java

     wlsuser 15160 15144  0  Feb 20  ?  17:27 /app1/jdk150_07/bin/IA64N/java -server -Xms1536m -Dplatform.home=/app1/bea/weblogic92 -Dwli.home=/app1/bea/weblogic92/integration

Desired output:

I am using HP-UX. Kindly help.

Try sth like this..

awk 'BEGIN{X[1]="java_home=";X[2]="wls_home="}
    {for(i=1;i<=NF;i++){if($i ~ /weblogic92$|java$/){split($i,P,"=");s=P[2]?P[2]:$i;print X[++a]""s}}}' file
1 Like

I guess you are using "weblogic92" as the parameter to trim the wls_home but I need to trim using "-Dplatform.home" because in other output weblogic92 may change to weblogic103 or something else while "-Dplatform.home" will always remain the same.

So I guess it will not help me.

so use Dplatform instead of weblogic...

awk 'BEGIN{X[1]="java_home=";X[2]="wls_home="}
    {for(i=1;i<=NF;i++){if($i ~ /-Dplatform\.home|java$/){split($i,P,"=");s=P[2]?P[2]:$i;print X[++a]""s}}}' file
1 Like

This is the output of the ps command as in the OP. I do not have any file to input to the awk. can you help with that change. I will be thankful .

You can use pipe for this...

just like this..

ps | awk {.....}
1 Like

Java_home and wls_home is populating correctly, however can you help me store them in bash variables? I am not good at awk.

Hi Mohtasims

What pamu given is working fine, however u asked for bash to store in variable....

weblogichome=`ps -ef |cut -d"=" -f2-3|awk '{print $1}'`
javahome=`ps -ef |cut -d"/" -f2-3|sed 's/^/\//g'`

I have used very basic commands to make ur output requested....

Thanks
Sha

I think you are not considering ""-Dplatform.home" and "java" to grep the weblogichome & jdkhome :(. I want them considered.