How to count how many times a word occurs in an XML file

I was entrusted with a quality check in the machine translation XML file. Translation is carried out from English to foreign. I have about 2000 translation blocks in the file, and I have to check 200 of them by adding my comments to the block enclosed in <comment> with the quality attribute. Is there a linux command or some kind of text editor that can count the number of added comments, or just the time when the word "/ comment" occurs, so I don’t need to track manually?

+3
source share
5 answers

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

+7

XSLT , XML:

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

XSLT XML , XSLT (, <?xml-stylesheet href="countComments.xsl" type="text/xsl"?>), XML .

+2

linux, , * nix, awk

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

, </comment> , grep -c "</comment>". :

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

ldap johnf.2010-02-12.log. 103 .

0

,

cat file | grep -c comment

-c 'count'.

0

Source: https://habr.com/ru/post/1732865/


All Articles