Your issue is that grep
returns multiple line matching your expression. What you end up executing with expr
is something like expr 1 3 4 + 4
(if there are matches on lines 1, 3, and 4). This is where your syntax error comes from.
From comments, it is clear that you want to comment out a particular child node from a particular ReconSummary
node. If you had wanted to delete e.g. the job
node, I would have suggested using
xmlstarlet ed -d '//ReconSummary[entryName = "Total Deep"]/job' file.xml >newfile.xml
... but commenting out rather than deleting is a bit more tricky.
Similar to an answer to your previous question that I wrote just moments ago, this would be done with an XSL transformation:
<xsl:transform 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="//ReconSummary[entryName = 'Total Deep']/job"> <xsl:text disable-output-escaping="yes"><!-- </xsl:text> <xsl:copy-of select="."/> <xsl:text disable-output-escaping="yes"> --></xsl:text> </xsl:template> </xsl:transform>
The XPATH query //ReconSummary[entryName = 'Total Deep']/job
matches the job
child node of the ReconSummary
node whose entryName
child node has the value Total Deep
.
You can change this easily to instead comment out the Tran
node. The XPATH query for that would be
//ReconSummary[entryName = 'Total Deep']/Tran`
To match both the job
and the Tran
node, use
//ReconSummary[entryName = 'Total Deep']/*[name()='job' or name()='Tran']
As in my previous answer, you would apply this XSL transformation to your XML file using either of
xmlstarlet tr transform.xsl file.xml >newfile.xml
or
xsltproc transform.xsl file.xml >newfile.xml
where you file is file.xml
and the transformation is stored in transform.xsl
.
grep -n "Deep" Formula.xml | cut -d: -f 1
is? In general, it would be better to parse the XML file using an XML parser, such asxmlstarlet
(but I'm a bit confused about what you're wanting XML line numbers for, as the XML format is not line oriented).expr
at the same time would be a problem? What you have is something likeexpr 39806 45608 + 9
. It would be better to tell us what the XML file is and what values needs to be extracted from it to achieve what you want to do. Usinggrep
is not the right approach to reading an XML file.<job>
tag?