Kris Kris - 1 year ago 270
Linux Question

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

I have an XML file with multiple lines like below.


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

Answer Source

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="" xmlns:exslt="" 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:value-of select="'&#10;'"/>
  <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="."/>

...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).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download