Claudiu Mihai
Claudiu Mihai

Reputation: 1

remove text in between tags on the same line

I have this line from an rrd file, where I need to replace what it is in between <v> and </v>.

<row><v> 2.0000000000e+02 </v><v> 2.3200000000e+02 </v><v> 2.6600000000e+02 </v></row>

In the above line, using patterns for the presented numbers, will work with:

|sed -r "s/<v> [0-9]+\.[0-9]+e\+[0-9]+ <\/v>/ NaN /gI".

My question is on how can you replace anything in between those tags (e.g with NaN) , not knowing what the text is:

<row><v> some text </v><v> .8234 </v><v> FA:DD:AB </v></row>

Upvotes: 0

Views: 83

Answers (2)

Michael Kay
Michael Kay

Reputation: 163352

Agree with choroba: you should always use XML-aware tools to process XML. In XSLT 3.0 this is

<xsl:transform version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:mode on-no-match="shallow-copy"/>
<xsl:template match="v/text()">NaN</xsl:template>
</xsl:transform>

Upvotes: 1

choroba
choroba

Reputation: 241928

It's better to use an XML-aware tool.

For example, in xsh, you'd write

open file.xml ;
for //v set . 'NaN' ;
save :b ;

Upvotes: 1

Related Questions