2016-09-07 111 views
0

我想从输出中提取一段文本。不知何故,我的正则表达式在记事本++中工作,但不在shell中。提取shell脚本中的文本

这里是输出:

-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: 

所以我在寻找:“-Duser.install.root=”参数,我想提取的值:“/apps/WebSphere/profiles85/node”。

输出中有2个。我必须采取一条路径并将其传递给另一个脚本。我用于提取我想要的文本的正则表达式。

“ - Duser.install.root=([\/\w\/]+?)”这个正则表达式在记事本中工作。

但我无法实现在shell脚本中检索文本。我希望我很清楚。

在此先感谢您的答案。

+0

你是如何尝试和使用的外壳,正则表达式? –

回答

0

使用sed:

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

在shell脚本:

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

输出:

/apps/WebSphere/profiles85i2/node 
+0

当我运行提供的命令时,感谢您的快速反馈,我仍然得到所有输出。而不是 输出: /apps/WebSphere/profiles85i2 /节点 – huso

+0

对不起,缺少'-n'选项和'uniq'命令只输出一次。我更新了。 – SLePort

+0

是的,现在没关系。 – huso