AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

RegEx or ?

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
er1c_net



Joined: 26 May 2007
Posts: 11

PostPosted: Tue Jul 08, 2008 2:55 pm    Post subject: RegEx or ? Reply with quote

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
View user's profile Send private message
engunneer



Joined: 30 Aug 2005
Posts: 6847
Location: Pacific Northwest, US

PostPosted: Tue Jul 08, 2008 4:36 pm    Post subject: Reply with quote

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

_________________
Unless otherwise noted, all code is untested.
Common Answers: 1.(Loops, Viruses, etc.) 2. Search 3.RTFM
Back to top
View user's profile Send private message Visit poster's website
pokercurious



Joined: 16 Dec 2007
Posts: 47

PostPosted: Tue Jul 08, 2008 6:31 pm    Post subject: Reply with quote

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
View user's profile Send private message
er1c_net



Joined: 26 May 2007
Posts: 11

PostPosted: Tue Jul 08, 2008 7:28 pm    Post subject: Reply with quote

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
View user's profile Send private message
pokercurious



Joined: 16 Dec 2007
Posts: 47

PostPosted: Tue Jul 08, 2008 11:29 pm    Post subject: Reply with quote

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
View user's profile Send private message
er1c_net



Joined: 26 May 2007
Posts: 11

PostPosted: Thu Jul 10, 2008 9:14 pm    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group