I have a XML tag for example
<tag name="abc"></tag>
I wanted to replace the string inside "" with the tag name i.e the upper XML tag should be become now
<abc name="abc"></abc>
I have a XML tag for example
<tag name="abc"></tag>
I wanted to replace the string inside "" with the tag name i.e the upper XML tag should be become now
<abc name="abc"></abc>
You shouldn't use something like sed/awk for it and use a xml/xslt processor, such as xmlstarlet instead.
Create a xslt file with a template such as and save it under transform.xsl:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="tag">
<xsl:element name="{@name}">
<xsl:attribute name="name">
<xsl:value-of select="@name"/>
</xsl:attribute>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
You can then apply the transformation on your xml document via
xmlstarlet tr transform.xsl input.xml
Do it as you prefer but here it is:
STR='<tag name="abc"></tag>'
AUX=$(echo $STR | cut -d"\"" -f2)
echo $STR | sed "s/tag/$AUX/g"
<tag class="foo" name="abc">? Or any of the many cases where there may be a " before the field you care about? Or tags that span multiple lines with the name on a different one? How would this be run on an XML file (which is presumably what the OP has)?
sed 's/name/abc/' so I think it's safe to assume the situation is a bit more complex than the example shown.