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

  • SEARCH
  • Home
  • 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 974869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:29:56+00:00 2026-05-16T03:29:56+00:00

I have a file log that I would like to parse and am having

  • 0

I have a file log that I would like to parse and am having some issues. At first it seemed it would be simple. I’ll go ahead and post the source I have come up with and then explain what I am trying to do.

The file I’m trying to parse contains this data:

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

My source code (below) basically breaks up the file line by line and then splits the line into two (key:value).

Source:

def dataList = [:]
def theInfoName = "C:\\testdata.txt"

File theInfoFile = new File(theInfoName)

def words
def key
def value

if (!theInfoFile.exists()) {
     println "File does not exist"

} else {

 theInfoFile.eachLine { line ->

 if (line.trim().size() == 0) {
  return null

 } else {

  words = line.split("\t: ")
  key=words[0] 
  value=words[1]
  dataList[key]=value

  println "${words[0]}=${words[1]}"
  }

 }
 println "$dataList.Performance"  //test if Performance has over-written the previous Performance value
}

The problem with my source is that when I use my getters (such as $dataList.Performance) it only shows the last one in the file rather than two.

So I’m wondering, how do I parse the file so that it keeps the information for both hard drives? Is there a way to pack the info into a ‘hard drive object’?

Any and all help is appreciated

A few side notes:

The file is on a windows machine (even though the info is grabbed from a nix system)

The text file is split by a tab, colon, and space (like shown in my source code) just thought I would state that because it doesn’t look like that on this page.

  • 1 1 Answer
  • 1 View
  • 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-16T03:29:57+00:00Added an answer on May 16, 2026 at 3:29 am

    This will read the data in blocks (with blank lines separating the blocks)

    def dataList = []
    def theInfoName = 'testdata.txt'
    
    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 ->
        // 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"
      }
    }
    

    Fingers crossed you have blank lines between your HDD info sections (you showed one in your test data) 🙂

    btw: I get the following output:

    Drive 0
        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
    Drive 1
        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
    

    Messing around, I also got the code down to:

    def dataList = []
    def theInfoFile = new File( 'testdata.txt' )
    
    if( !theInfoFile.exists() ) {
      println "File does not exist"
    } else {
      // Split the text of the file into blocks separated by \n\n
      // Then, starting with an empty list go through each block of text in turn
      dataList = theInfoFile.text.split( '\n\n' ).inject( [] ) { list, block ->
        // Split the current block into lines (based on the newline char)
        // Then starting with an empty map, go through each line in turn
        // when done, add this map to the list we created in the line above
        list << block.split( '\n' ).inject( [:] ) { map, line ->
          // Split the line up into a key and a value (trimming each element)
          def (key,value) = line.split( '\t: ' ).collect { it.trim() }
          // Then, add this key:value mapping to the map we created 2 lines above
          map << [ (key): value ] // The leftShift operator also returns the map 
                                  // the inject closure has to return the accumulated
                                  // state each time the closure is called
        }
      }
    }
    
    dataList.eachWithIndex { it, index ->
      println "Drive $index"
      it.each { k, v ->
        println "\t$k = $v"
      }
    }
    

    But that has to load the whole file into memory at once (and relies on \n as the EOL termination char)

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

Sidebar

Related Questions

Perl newbie here. I have a log file that I need to parse out
I've rm 'ed a 2.5gb log file - but it doesn't seemed to have
I'm looking for some clarity on the SQL Server log file. I have a
I have a file which is an XML representation of some data that is
So, for some strange reason I end up with a 100GB log file that
I have apache running on different servers, I would like to rsync log files
I would like to read a (fairly big) log file into a MATLAB string
I have a windows service that writes out log file entries to an XML
I have a simple log file which is very messy and I need it
i have a log file which contains hundreds/thousands of seperate XML messages and need

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.