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.
This will read the data in blocks (with blank lines separating the blocks)
Fingers crossed you have blank lines between your HDD info sections (you showed one in your test data) :-)
btw: I get the following output:
Messing around, I also got the code down to:
But that has to load the whole file into memory at once (and relies on
\n
as the EOL termination char)Here is my solution: