Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 990931
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:01:35+00:00 2026-05-16T06:01:35+00:00

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

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T06:01:35+00:00Added an answer on May 16, 2026 at 6:01 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a follow-on question from the one I asked here . Can constraints
This is kind of a follow-up question to a question I asked over here
Disclaimer: I know this type of question has been asked here before, I just
This question is a follow-up to one I asked and got answered here: How
This question is a follow on from the one that was asked here .
This is a follow up to a question I asked earlier seen here: Confused
I know this question has been asked many times. But from what I read,
This is a follow-up question to this question I asked earlier. Btw thanks Neil
This is a follow up question to a previous question I asked about calculating
This question is kind of a follow up to this question I asked a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.