AutoHotkey Community

It is currently May 26th, 2012, 8:54 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: RegEx or ?
PostPosted: July 8th, 2008, 3:55 pm 
Offline

Joined: May 26th, 2007, 11:53 pm
Posts: 14
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!


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 8th, 2008, 5:36 pm 
Offline
User avatar

Joined: August 30th, 2005, 8:43 pm
Posts: 8666
Location: Salem, MA
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

_________________
Image
(Common Answers) - New Tutorials Forum - Humongous FAQ


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 8th, 2008, 7:31 pm 
Offline

Joined: December 16th, 2007, 12:55 pm
Posts: 48
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"


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 8th, 2008, 8:28 pm 
Offline

Joined: May 26th, 2007, 11:53 pm
Posts: 14
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...


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 9th, 2008, 12:29 am 
Offline

Joined: December 16th, 2007, 12:55 pm
Posts: 48
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 10th, 2008, 10:14 pm 
Offline

Joined: May 26th, 2007, 11:53 pm
Posts: 14
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!


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: kkkddd1, poserpro, Yahoo [Bot] and 66 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group