SED错误-`s'命令的RHS提取XML文本的参考\ 1无效 [英] SED error - invalid reference \1 on `s' command's RHS extracting XML text
问题描述
我有一个包含多行的XML文件,如下所示.
I have an XML file with multiple lines like below.
<sandbox>false</sandbox>
<serverUrl>https://salesforce.com/services/Soap/u/37.0/</serverUrl>
<sessionId>00D4100000087K9!AQMAQJElzjgvA01eaCo</sessionId>
<userId>00541000000JOzJAAW</userId>
<userInfo>
我试图在Linux上使用sed在两个sessionId标记之间获取一个值.
I am trying to use sed on Linux to get a value between the two sessionId tags.
sed -n '/<sessionId>.*$/{s/<sessionId>.*<\/sessionId>/\1/;p}' LoginResponse.xml
但是它抛出以下错误. 任何建议请...
But it is throwing the below error. Any suggestions please...
sed: -e expression #1, char 50: invalid reference \1 on `s' command's RHS
推荐答案
正确的事情
The Right Thing
Don't use sed
for this at all; XML is not a regular language, so regular expressions are categorically not powerful enough to parse it correctly. Your current code can't distinguish a comment that talks about sessionId tags from a real sessionId tag; can't recognize element encodings; can't deal with unexpected attributes being present on your tag; etc.
相反,请使用:
xmlstarlet sel -t -m '//sessionId' -v . -n < LoginResponse.xml
...或者,如果您没有XMLStarlet,则可以使用XSLTProc(在现代UNIXy系统上,它几乎是开箱即用的通用).如果将以下内容另存为extract-session-id.xslt
:
...or, if you don't have XMLStarlet, you can use XSLTProc (which is almost universally available out-of-the-box on modern UNIXy systems). If you save the following as extract-session-id.xslt
:
<?xml version="1.0"?>
<!-- this was generated with:
-- xmlstarlet sel -C -t -m '//sessionId' -v . -n
-->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:exslt="http://exslt.org/common" version="1.0" extension-element-prefixes="exslt">
<xsl:output omit-xml-declaration="yes" indent="no"/>
<xsl:template match="/">
<xsl:for-each select="//sessionId">
<xsl:call-template name="value-of-template">
<xsl:with-param name="select" select="."/>
</xsl:call-template>
<xsl:value-of select="' '"/>
</xsl:for-each>
</xsl:template>
<xsl:template name="value-of-template">
<xsl:param name="select"/>
<xsl:value-of select="$select"/>
<xsl:for-each select="exslt:node-set($select)[position()>1]">
<xsl:value-of select="' '"/>
<xsl:value-of select="."/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
...然后您可以运行xsltproc extract-session-id.xslt LoginResponse.xml
来获取输出.
...then you can run xsltproc extract-session-id.xslt LoginResponse.xml
to get your output.
也就是说,关于您的sed
错误:您需要传递-r
来启用ERE语法:
That said, with respect to your sed
bug: You need to pass -r
to enable ERE syntax:
# requires GNU sed for -r
sed -r -n -e '/<sessionId>.*$/{s/<sessionId>(.*)<\/sessionId>/\1/;p}'
或者,通过MacOS BSD sed,还需要进行其他一些调整:
Alternately, with the MacOS BSD sed, some other tweaks are needed:
# -E, not -r, on MacOS BSD sed; semicolon between "p", "}" needed.
sed -E -n '/<sessionId>.*$/ { s/<sessionId>(.*)<\/sessionId>/\1/; p; }'
如果您的会话ID曾经在元素后面包含字符,这将表现不佳-&
看起来像&
,以此类推;因此,使用适当的XML解析器是更安全的选择. (同样,如果内容曾经更改过<sessionid type="foo">...</sessionid>
,或者在发生任何其他方式的更改的情况下.)
This will behave badly if your session IDs ever include characters that are behind elements -- &
s will look like &
and so forth; using a proper XML parser is thus the safer option. (Likewise, if the content ever changed so <sessionid type="foo">...</sessionid>
, or in the event of any manner of other changes).
这篇关于SED错误-`s'命令的RHS提取XML文本的参考\ 1无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!