Groovy read text file but omit header

2019-07-29 10:36发布

This is a follow up to the question asked here: Groovy parsing text file

The difference now is my file has a header, I attempted to read past the header first and then onto the content that I am wanting, but for some reason it doesn't seem to cooperate.

def dataList = []
def theInfoName = 'testdata.txt'
boolean headersDone = false    //header set to false by default

File theInfoFile = new File( theInfoName )

if( !theInfoFile.exists() ) {
  println "File does not exist"
} else {
  def driveInfo = [:]
  // Step through each line in the file
  theInfoFile.eachLine { line ->

  //this is where im trying to account for the header
  if(!headersDone) {      //look if line contains "..." if it does that turn headersDone to true
   if(line.contains("...")) {
     headersDone = true
   }
  } else {
     // If the line isn't blank
     if( line.trim() ) {
       // Split into a key and value
       def (key,value) = line.split( '\t: ' ).collect { it.trim() }
       // and store them in the driveInfo Map
       driveInfo."$key" = value
     }
     else {
       // If the line is blank, and we have some info
       if( driveInfo ) {
         // store it in the list
         dataList << driveInfo
         // and clear it
        driveInfo = [:]
       }
     }
  }
  // when we've finished the file, store any remaining data
  if( driveInfo ) {
    dataList << driveInfo
  }
}

dataList.eachWithIndex { it, index ->
  println "Drive $index"
  it.each { k, v ->
    println "\t$k = $v"
  }
}

I tried it out with the code provided in the previous post to make sure it wasn't something I was doing differently and it came with the same output.

What happens is that it posts the same block of information 11 times.

The header looks is the following:

Random date information here with some other info
Slightly more random information followed by

Examining hard disk information ...

HDD Device 0 : /dev/sda
HDD Model ID  : ST3160815A
HDD Serial No : 5RA020QY
HDD Revision  : 3.AAA
HDD Size     : 152628 MB
Interface    : IDE/ATA
Temperature         : 33 C
Health  : 100%
Performance  : 70%
Power on Time : 27 days, 13 hours
Est. Lifetime : more than 1000 days

HDD Device 1 : /dev/sdb
HDD Model ID  : TOSHIBA MK1237GSX
HDD Serial No : 97LVF9MHS
HDD Revision  : DL130M
HDD Size     : 114473 MB
Interface    : S-ATA
Temperature  : 30 C
Health  : 100%
Performance  : 100%
Power on Time : 38 days, 11 hours
Est. Lifetime : more than 1000 days

Does anyone know why it is printing out duplicate of the information?

2条回答
欢心
2楼-- · 2019-07-29 11:14

Can't see anything obviously wrong with the code. I suggest to add a couple of printlns so you can see how the maps, lists and variables change. That might give you an idea where the bug might be.

查看更多
来,给爷笑一个
3楼-- · 2019-07-29 11:23

The problem is the addition of the "last" driveInfo to the dataList:

// when we've finished the file, store any remaining data
if( driveInfo ) {
   dataList << driveInfo
}

It has to be one curly brace below its current position, otherwise it belongs to the eachLine closure.

查看更多
登录 后发表回答