SED error - invalid reference \1 on `s' command's RHS extracting XML text

拥有回忆 提交于 2019-12-02 14:23:17

问题


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>

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

回答1:


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.

Instead, use:

xmlstarlet sel -t -m '//sessionId' -v . -n < LoginResponse.xml

...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="'&#10;'"/>
    </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()&gt;1]">
      <xsl:value-of select="'&#10;'"/>
      <xsl:value-of select="."/>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

...then you can run xsltproc extract-session-id.xslt LoginResponse.xml to get your output.


The sed Thing

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}'

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; }'

This will behave badly if your session IDs ever include characters that are behind elements -- &s will look like &amp; 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).




回答2:


For a reference to work you have to tell said what to refer to using round brackets, which you have none.

Try this link :- http://www.grymoire.com/Unix/Sed.html#uh-4



来源:https://stackoverflow.com/questions/39667461/sed-error-invalid-reference-1-on-s-commands-rhs-extracting-xml-text

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!