 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
er1c_net
Joined: 26 May 2007 Posts: 14
|
Posted: Tue Jul 08, 2008 2:55 pm Post subject: RegEx or ? |
|
|
I have a directory that has about 400 text files in it. The information I need to search for appears to always be on the 4th line of one of the text files. (It is log in information and the variables I need to display are username, computer name, and date/time.
| Code: | | 08:19:12 - INFO: JSmith working on CLELTL3B8287. |
What I would like to do is; pull out the "JSmith", "CLELTL3B8287", and "08:19:12" and display that.
Would I want to use RegEx to pull out those fields?
Thanks! |
|
| Back to top |
|
 |
engunneer
Joined: 30 Aug 2005 Posts: 8255 Location: Maywood, IL
|
Posted: Tue Jul 08, 2008 4:36 pm Post subject: |
|
|
you could, if you know regex or want to learn it. I still have quite a hard time from it.
Use FileReadLine to get just the 4th line, then the following (which is one of many many ways to do it):
| Code: |
; for testing
infoFromFile = 08:19:12 - INFO: JSmith working on CLELTL3B8287.
;parse
StringReplace, infoFromFile, infoFromFile, - INFO:, *
StringReplace, infoFromFile, infoFromFile, working on, *
StringTrimRight, infoFromFile, infoFromFile, 1 ;remove the .
StringSplit, tmp, infoFromFile, *, %A_Space%
log_time := tmp1
log_name := tmp2
log_device := tmp3
|
_________________
(Common Answers) |
|
| Back to top |
|
 |
pokercurious
Joined: 16 Dec 2007 Posts: 48
|
Posted: Tue Jul 08, 2008 6:31 pm Post subject: |
|
|
using named regexes (and assuming you've gotten the relevant line of text into a variable AND assuming that the line is always in the same format):
| Code: | ; for testing
infoFromFile = 08:19:12 - INFO: JSmith working on CLELTL3B8287.
; regex
log_regex := "(?P<time>(\d{2}):(\d{2}):(\d{2})) - INFO: (?P<name>\w+) working on (?P<device>\w+)\."
RegExMatch(infoFromFile, log_regex, log_)
; ---RESULTS---
; log_device: "CLELTL3B8287"
; log_name: "JSmith"
; log_time: "08:19:12" |
|
|
| Back to top |
|
 |
er1c_net
Joined: 26 May 2007 Posts: 14
|
Posted: Tue Jul 08, 2008 7:28 pm Post subject: |
|
|
| well, the thing is, I have to scan through approximately 400 log files to find the correct one (based on device id). I guess I need to loop and read each file and then when I get a match, then parse out that data... |
|
| Back to top |
|
 |
pokercurious
Joined: 16 Dec 2007 Posts: 48
|
Posted: Tue Jul 08, 2008 11:29 pm Post subject: |
|
|
| Code: | ; the RE we'll use to get the info we want
log_regex := "(?P<time>(\d{2}):(\d{2}):(\d{2})) - INFO: (?P<name>\w+) working on (?P<device>\w+)\."
; get the device number
InputBox, device_number, Enter the device number, Enter the device number
If ErrorLevel
ExitApp
; select the folder containing the log files
FileSelectFolder, log_folder, *%A_WorkingDir%, , Select the folder with the log files
If ErrorLevel
ExitApp
; loop through the files, looking for the device number
; when found, extract the appropriate information
Loop, %log_folder%\*.*, , 1
{
Loop, Read, %A_LoopFileFullPath%
{
IfInString, A_LoopReadLine, %device_number%
{
RegExMatch(A_LoopReadLine, log_regex, log_)
}
}
}
; voila!
msgbox log_device: %log_device%`nlog_name: %log_name%`nlog_time: %log_time% |
Not tested - if it doesn't work, it should at least give you an idea of the direction in which to proceed. |
|
| Back to top |
|
 |
er1c_net
Joined: 26 May 2007 Posts: 14
|
Posted: Thu Jul 10, 2008 9:14 pm Post subject: |
|
|
pokercurious, thanks much! That gives me a direction to head in. I think with some tweaks it will work. The searching and looping was driving me crazy...
Thanks again to everyone also! I appreciate the help! |
|
| Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|