huso huso - 3 months ago 12
Bash Question

extract the text within shell script

I am trying to extract piece of text from an output. Somehow my regex is working in notepad++ but not in the shell.

Here is the output:

-Duser.instal-Djava.library.path=/apps/WebSphere/AppServer85i2/lib/native/aix/ppc_64/:/app
s/WebSphere/AppServer85i2/java/jre/lib/ppc64/compressedrefs:/apps/WebSphere/AppServer85i2/java/jre/lib/ppc64:/apps/WebSphere/AppServer85i2/java/jre/lib/ppc64:/apps/WebSphere/Ap
pServer85i2/java/jre/lib/ppc64/compressedrefs:/apps/WebSphere/AppServer85i2/java/jre/lib/ppc64/j9vm:/apps/WebSphere/AppServer85i2/java/jre/lib/ppc64:/apps/WebSphere/AppServer85
i2/java/jre/../lib/ppc64:/apps/WebSphere/AppServer85i2/bin:/apps/WebSphere/AppServer85i2/nulldllsdir:/usr/lib:/usr/lib: -Djava.endorsed.dirs=/apps/WebSphere/AppServer85i2/endor
sed_apis:/apps/WebSphere/AppServer85i2/java/jre/lib/endorsed -Duser.install.root=/apps/WebSphere/profiles85i2/node -Djava.security.auth.login.config=/apps/WebSphere/profiles85i2/node/properties/wsjaas.conf -Djava.security.policy=/
apps/WebSphere/profiles85i2/node/properties/server.policy com.ibm.wsspi.bootstrap.WSPreLauncher -nosplash -application com.ibm.ws.bootstrap.WSLauncher com.ibm.ws.runtime.WsServ
er /apps/WebSphere/profiles85i2/node/config hello hello2 PROJ1_LSDS-1
root 2057l -Dcom.ibm.xtq.processor.overrideSecureProcessing=true -Xbootclasspath/p:/apps/WebSphere/AppSer
ver85i2/java/jre/lib/ibmorb.jar -classpath /apps/WebSphere/profiles85i2/node/properties:/apps/WebSphere/AppServer85i2/properties:/apps/WebSphere/AppServer85i2/lib/startup.jar:/
apps/WebSphere/AppServer85i2/lib/bootstrap.jar:/apps/WebSphere/AppServer85i2/lib/jsf-nls.jar:/apps/WebSphere/AppServer85i2/lib/lmproxy.jar:/apps/WebSphere/AppServer85i2/lib/url
protocols.jar:/apps/WebSphere/AppServer85i2/deploytool/itp/batchboot.jar:/apps/WebSphere/AppServer85i2/deploytool/itp/batch2.jar:/apps/WebSphere/AppServer85i2/java/lib/tools.ja
r -Dibm.websphere.internalClassAccessMode=allow -Xms50m -Xmx256m -Xcompressedrefs -Xscmaxaot4M -Xscmx90M -Dws.ext.dirs=/apps/WebSphere/AppServer85i2/java/lib:/apps/WebSphere/pr
ofiles85i2/node/classes:/apps/WebSphere/AppServer85i2/classes:/apps/WebSphere/AppServer85i2/lib:/apps/WebSphere/AppServer85i2/installedChannels:/apps/WebSphere/AppServer85i2/li
b/ext:/apps/WebSphere/AppServer85i2/web/help:/apps/WebSphere/AppServer85i2/deploytool/itp/plugins/com.ibm.etools.ejbdeploy/runtime -Dderby.system.home=/apps/WebSphere/AppServer
85i2/derby -Dcom.ibm.itp.location=/apps/WebSphere/AppServer85i2/bin -Djava.util.logging.configureByServer=true -Duser.install.root=/apps/WebSphere/profiles85i2/node -Djava.ext.
dirs=/apps/WebSphere/AppServer85i2/tivoli/tam:/apps/WebSphere/AppServer85i2/java/jre/lib/ext -Djavax.management.builder.initial=com.ibm.ws.management.PlatformMBeanServerBuilder
-Dpython.cachedir=/apps/WebSphere/profiles85i2/node/temp/cachedir -Dwas.install.root=/apps/WebSphere/AppServer85i2 -Djava.util.logging.manager=com.ibm.ws.bootstrap.WsLogManage
r -Dserver.root=/apps/WebSphere/profiles85i2/node -Dcom.ibm.security.jgss.debug=off -Dcom.ibm.security.krb5.Krb5Debug=off -Dcom.interwoven.livesite.fileappender.root=/apps/WebS
phere/profiles85i2/node/logs/PROJ1PP_LSDS-1 -Dlog4j.configuration=log4j.xml -Dlog4j.debug=true -Djava.library.path=/apps/WebSphere/AppServer85i2/lib/native/aix/ppc_64/:/apps/We
bSphere/AppServer85i2/java/jre/lib/ppc64/compressedrefs:/apps/WebSphere/AppServer85i2/java/jre/lib/ppc64:/apps/WebSphere/AppServer85i2/java/jre/lib/ppc64:/apps/WebSphere/AppSer
ver85i2/java/jre/lib/ppc64/compressedrefs:/apps/WebSphere/AppServer85i2/java/jre/lib/ppc64/j9vm:/apps/WebSphere/AppServer85i2/java/jre/lib/ppc64:/apps/WebSphere/AppServer85i2/j
ava/jre/../lib/ppc64:/apps/WebSphere/AppServer85i2/bin:/apps/WebSphere/AppServer85i2/nulldllsdir:/usr/lib:/usr/lib:


So I am looking for: "
-Duser.install.root=
" parameter and I want to extract the value:"
/apps/WebSphere/profiles85/node
".

There are 2 of them in the output. I have to take one path and pass it to another script. The regex that I use for extract the text that I want.

"-
Duser.install.root=([\/\w\/]+?)
" this regex works in the notepad.

but I couldn't realize to retrieve the text within shell script. I hope I was clear.

Thanks in advance for your answers.

Answer

With sed:

$ sed -n "s/.*-Duser.install.root=\([^ ]*\).*/\1/p" file | uniq
/apps/WebSphere/profiles85i2/node

in shell script:

res=$(sed -n "s/.*-Duser.install.root=\([^ ]*\).*/\1/p" file | uniq)
echo "$res"

output:

/apps/WebSphere/profiles85i2/node