| View previous topic :: View next topic |
| Author |
Message |
twhyman
Joined: 07 Dec 2005 Posts: 218
|
Posted: Tue May 15, 2007 8:21 am Post subject: Regex help in breaking a line into subpattern |
|
|
Hi,
i want to capture and store hard drive s.m.a.r.t data into subpatterns
i tried using an array but it didnt work universally for every line.
| Code: |
ID Attribute Type Threshold Value Worst Raw Status
---- -------------------------- ----- --------- ----- ----- ---------- --------
[01] Raw Read Error Rate Prefailure 51 200 200 11 OK
[03] Spin Up Time Prefailure 21 188 187 1558 OK
[04] Start/Stop Count Advisory 0 100 100 673 OK
[05] Reallocated Sector Count Prefailure 140 200 200 0 OK
[07] Seek Error Rate Advisory 0 200 200 0 OK
[09] Power On Hours Count Advisory 0 99 99 1202 OK
[0A] Spin Retry Count Advisory 0 100 100 0 OK
[0B] Calibration Retry Count Advisory 0 100 100 0 OK
[0C] Power Cycle Count Advisory 0 100 100 554 OK
[C0] Power-Off Park Count Advisory 0 200 200 451 OK
[C1] Load/Unload Cycle Count Advisory 0 190 190 31415 OK
[C2] Drive Temperature Advisory 0 110 105 37 OK
[C4] Re-Allocated Data Count Advisory 0 200 200 0 OK
[C5] Pending Sector Count Advisory 0 200 200 0 OK
[C6] UnCorrectable Sector Count Advisory 0 200 200 0 OK
[C7] CRC Error Count Advisory 0 200 200 0 OK
[C8] Write Error Rate Advisory 0 200 200 0 OK
|
thanks
Twhyman |
|
| Back to top |
|
 |
Helpy Guest
|
Posted: Tue May 15, 2007 9:34 am Post subject: |
|
|
Since your data seems to be well aligned, I believe parsing with SubStr is an easy way.
Otherwise, you can use stuff like: ".{4} .{50}" and so on, to get the split in one go. |
|
| Back to top |
|
 |
twhyman
Joined: 07 Dec 2005 Posts: 218
|
Posted: Sat May 19, 2007 7:32 am Post subject: |
|
|
helpy,
can you give an example cause i dont realy follow you?
thanks
twhyman |
|
| Back to top |
|
 |
atnbueno
Joined: 24 Mar 2007 Posts: 26
|
Posted: Sat May 19, 2007 10:14 am Post subject: |
|
|
Hi,
Helpy means that you can use the fact that every part of your data can be considered to start at a particular column/position. Discarding the header rows, your data follows this pattern:
ID: starts at position 2, has a length of 2
Attribute: starts at position 6, has a length of 26
Type: starts at position 33, has a length of 10
Threshold: starts at position 44, has a length of 4
Value: starts at position 49, has a length of 5
Worst: starts at position 55, has a length of 5
Raw: starts at position 61, has a length of 10
Status: starts at position 72, has a length of 8
Each one of these can be directly translated into a SubStr() or a StringMid command. Then you'll have to deal with the spaces left and right.
On the other side, a RegEx would allow you to process each line with a single command (including the space removal). _________________ Regards,
Antonio |
|
| Back to top |
|
 |
PhiLho
Joined: 27 Dec 2005 Posts: 6721 Location: France (near Paris)
|
Posted: Sat May 19, 2007 11:51 am Post subject: |
|
|
| atnbueno wrote: | | Then you'll have to deal with the spaces left and right. | Not if he uses StringMid with AutoTrim on (the default). _________________
vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2") |
|
| Back to top |
|
 |
atnbueno
Joined: 24 Mar 2007 Posts: 26
|
Posted: Sat May 19, 2007 12:50 pm Post subject: |
|
|
You're right. I forgot about AutoTrim. _________________ Regards,
Antonio |
|
| Back to top |
|
 |
twhyman
Joined: 07 Dec 2005 Posts: 218
|
Posted: Sat May 19, 2007 4:23 pm Post subject: |
|
|
thanks for the help guys, but i am looking for a single regex line to procces the lines.
i tried for hours to get it but with no succes
can you help me with the regex (maybe PhiLho the RegEx master )?
Thanks
Twhyman |
|
| Back to top |
|
 |
PhiLho
Joined: 27 Dec 2005 Posts: 6721 Location: France (near Paris)
|
Posted: Sat May 19, 2007 5:45 pm Post subject: |
|
|
OK, it isn't so hard, just need to be clean:
| Code: | lines =
(
ID Attribute Type Threshold Value Worst Raw Status
---- -------------------------- ----- --------- ----- ----- ---------- --------
[01] Raw Read Error Rate Prefailure 51 200 200 11 OK
[03] Spin Up Time Prefailure 21 188 187 1558 OK
[04] Start/Stop Count Advisory 0 100 100 673 OK
[05] Reallocated Sector Count Prefailure 140 200 200 0 OK
[07] Seek Error Rate Advisory 0 200 200 0 OK
[09] Power On Hours Count Advisory 0 99 99 1202 OK
[0A] Spin Retry Count Advisory 0 100 100 0 OK
[0B] Calibration Retry Count Advisory 0 100 100 0 OK
[0C] Power Cycle Count Advisory 0 100 100 554 OK
[C0] Power-Off Park Count Advisory 0 200 200 451 OK
[C1] Load/Unload Cycle Count Advisory 0 190 190 31415 OK
[C2] Drive Temperature Advisory 0 110 105 37 OK
[C4] Re-Allocated Data Count Advisory 0 200 200 0 OK
[C5] Pending Sector Count Advisory 0 200 200 0 OK
[C6] UnCorrectable Sector Count Advisory 0 200 200 0 OK
[C7] CRC Error Count Advisory 0 200 200 0 OK
[C8] Write Error Rate Advisory 0 200 200 0 OK
)
Loop Parse, lines, `n, `r
{
fp := RegExMatch(A_LoopField, "\[(?'ID'..)]\s(?'Attribute'.{27})(?'Type'\w+)\s+(?'Threshold'\d+)\s+(?'Value'\d+)\s+(?'Worst'\d+)\s+(?'Raw'\d+)\s+(?'Status'\w+)", field)
If (fp > 0)
{
fieldAttribute = %fieldAttribute% ; Trim spaces
MsgBox ID: %fieldID% Attribute: %fieldAttribute% Type: %fieldType% Threshold: %fieldThreshold% Value: %fieldValue% Worst: %fieldWorst% Raw: %fieldRaw% Status: %fieldStatus%
}
}
| [EDIT] Took in account atnbueno's remarks. _________________
vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")
Last edited by PhiLho on Sat May 19, 2007 8:32 pm; edited 2 times in total |
|
| Back to top |
|
 |
atnbueno
Joined: 24 Mar 2007 Posts: 26
|
Posted: Sat May 19, 2007 7:31 pm Post subject: |
|
|
There's a
| Code: | | (?'Threshold'\d+)\s+ |
missing in the RegEx and I would left the square brackets outside the ID but other than that, thanks for the code, a very clarifying example.
I hadn't noticed the problem with "Attribute" and the spaces  _________________ Regards,
Antonio |
|
| Back to top |
|
 |
PhiLho
Joined: 27 Dec 2005 Posts: 6721 Location: France (near Paris)
|
Posted: Sat May 19, 2007 8:33 pm Post subject: |
|
|
Good remarks, thanks. I corrected the code accordingly. _________________
vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2") |
|
| Back to top |
|
 |
twhyman
Joined: 07 Dec 2005 Posts: 218
|
Posted: Sun May 20, 2007 2:04 am Post subject: |
|
|
thanks alot guys for the help  |
|
| Back to top |
|
 |
|