Reputation: 314
I have a log file what contains S.M.A.R.T. data of my hard drive. I would like to handle this file with PowerShell. Here is the part of my log file.
3 Spin_Up_Time 0x0020 100 100 000 Old_age Offline - 0
4 Start_Stop_Count 0x0030 100 100 000 Old_age Offline - 0
5 Reallocated_Sector_Ct 0x0032 100 100 000 Old_age Always - 0
And here is my code
$i = 1
$a = Get-Content log.txt
do {
$trimmed = $a[$i].trim()
$splitted = $trimmed.split(" ")
$i++
}while ($i -le 3)
If I use the .split(" ") it is working only with the thrid row. How can I split my all rows correctly?
Thank you
Upvotes: 0
Views: 405
Reputation: 604
I like working with regex' here's a sample that allows you to name your columns.
$a = Get-Content log.txt
$pattern = [regex]'(?<rowid>\d+)\s(?<desc>[a-zA-Z_]+)\s+(?<hexdata>0x\d{4})\s+(?<col4>\d{3})\s+(?<col5>\d{3})\s+(?<col6>\d{3})\s+(?<text1>.+?)\s+(?<state>.+?)-\s+0'
foreach ($line in $a) {
if ($line -match $pattern) {
$dataobj = New-Object PSObject
$dataobj | Add-Member -type NoteProperty -name "Description" -value $matches['desc']
$dataobj | Add-Member -type NoteProperty -name "Hex Data" -value $matches['hexdata']
$dataobj | Add-Member -type NoteProperty -name "State" -value $matches['state']
$dataobj
}
}
Results:
Description Hex Data State
----------- -------- -----
Spin_Up_Time 0x0020 Offline
Start_Stop_Count 0x0030 Offline
Reallocated_Sector_Ct 0x0032 Always
Upvotes: 0
Reputation: 36342
A bit more code, but it gives you something that's a little easier to work with in the end:
$SMART = gc c:\temp\test.txt | %{
$temp = $_ -split " "|?{!([string]::IsNullOrWhiteSpace($_))}
new-object psobject -Property @{
"Entry"=$temp[0]
"TestName"=$temp[1]
"HexCode"=$temp[2]
"Number1"=$temp[3]
"Number2"=$temp[4]
"Number3"=$temp[5]
"Age"=$temp[6]
"Status"=$temp[7]
"Filler"=$temp[8]
"Zero?"=$temp[9]
}
}
$SMART|FT Entry,TestName,HexCode,Number1,Number2,Number3,Age,Status,Filler,Zero?
Upvotes: 2
Reputation: 6635
what does this do for you?
$a = Get-Content log.txt
-split $a
I get this
H:\> -split $a
3
Spin_Up_Time
0x0020
100
100
000
Old_age
Offline
-
0
4
Start_Stop_Count
0x0030
100
100
000
Old_age
Offline
-
0
5
Reallocated_Sector_Ct
0x0032
100
100
000
Old_age
Always
-
0
Upvotes: 1