文件中的Groovy grep单词

时间:2017-09-18 06:24:49

标签: groovy

我想在path的文件中grep单词。怎么用groovy方式?如何统计我找到每个文件的单词数量?

import groovy.io.FileType

def splitStatements() {
     String path = "C:\\Users\\John\\test"
     def result = new AntBuilder().fileset( dir: path ) {
          containsregexp expression:['END','BEGIN']
     }*.file
println result
}
splitStatements()

1 个答案:

答案 0 :(得分:0)

这就是我想要的:

def wordCount_END = 0
    def wordCount_BEGIN = 0
    def dir = new File("C:\\Users\\John\\test")

dir.eachFileRecurse (FileType.FILES) { file ->
   Scanner s = new Scanner(file) 
   while (s.hasNext()) {
       if (s.next().equals('BEGIN')) wordCount_END++
       }
  }     
dir.eachFileRecurse (FileType.FILES) { file ->
   Scanner s = new Scanner(file) 
   while (s.hasNext()) {
       if (s.next().equals('END')) wordCount_BEGIN++    
       }


}
          println("END count per lock:  " + wordCount_END)
          println("BEGIN count per lock:  " + wordCount_BEGIN)      

}