views:

178

answers:

5

I was given the task of doing quality check on a machine translation xml file. The translations are from English to a foreign language. I have about 2000 translation blocks in the file and I have to check 200 of them by adding my remarks in the block enclosed in a < comment > tag with a quality attribute. Is there a linux command or some text editor out there which can count the number of comment tags I add or just the number of time the word '/comment' occurs so I don't have to keep track manually?

A: 

your tag says linux, so i assume you have *nix tools like awk

awk '{for(i=1;i<=NF;i++){if($i=="/comment"){++c} } }END{print "total: "c}' xmlfile
ghostdog74
A: 

If you know that the </comment> doesn't occur more than once per line, just use grep -c "</comment>". Example:

[~/.logs]> grep -c ldap johnf.2010-02-12.log
103

This searches for the string ldap in the file johnf.2010-02-12.log. The string appears on 103 distinct lines.

John Feminella
+4  A: 

grep '/comment' yourfile.xml -o | wc -l

Rob H
A: 

As long as the comments appear on their own line, you could try

cat file | grep -c comment

The -c stands for 'count'.

Matt Luongo
UUOC. `grep -c comment file`
ghostdog74
+2  A: 

This XSLT stylesheet can be run on any platform and will tell you how many comment elements there are in the XML document:

<?xml version="1.0" encoding="UTF-8"?>
  <xsl:stylesheet
   version="1.0"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
  <xsl:output method="text" encoding="UTF-8" omit-xml-declaration="yes"/>
  <xsl:template match="/">
    <xsl:value-of select="count(//comment)"/>
  </xsl:template>
</xsl:stylesheet>

If you add a XSLT processing instruction at the top of the XML file that points to this XSLT( e.g. <?xml-stylesheet href="countComments.xsl" type="text/xsl"?> ), then you could just load the XML file in a browser and see the number displayed.

Mads Hansen