pk7272 Posted June 9, 2016 Share Posted June 9, 2016 i have a unique id in placed in a log file, i can search the file and get to it, once i find the unique id in the file i need to find another string (naming it string 2) after this unique id and get to the very next line of the string 2 . Please find below my function and please suggest how to achieve this . Func getAuthResponse($LogfilePath, $AuthRespFilePath, $UniqueId, $search) Global $iLine = 0, $sLine = '' Global $hFile = FileOpen($LogfilePath) If $hFile = -1 Then MsgBox(0,'ERROR','Unable to open file for reading.') Exit 1 EndIf ;If $hFile = -1 Then ; find the line that has the search string While 1 $iLine += 1 $sLine = FileReadLine($hFile) If @error = -1 Then ExitLoop ; finding the unique id in the log file ;ConsoleWrite($UniqueId & @LF) If StringInStr($sLine, $UniqueId) Then ConsoleWrite($sLine & @LF) ; assuming that unique id is found , now finding the phrase Auth response is as follow : after the unique id $sNewLine = $sLine+ If StringInStr($sLine, $search) Then ConsoleWrite($sLine & @LF) //// SOME LOGIC //// ExitLoop EndIf ;If StringInStr($sLine, $search) Then ExitLoop EndIf ;If(StringInStr($sLine, $UniqueId) Then WEnd ;While 1 FileClose($hFile) EndFunc Thanks & Regards, PK Link to comment Share on other sites More sharing options...
jchd Posted June 9, 2016 Share Posted June 9, 2016 Please post sample input (masquerade private data is any). Use of FileRead and an ad hoc regexp will surely do. This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt) Link to comment Share on other sites More sharing options...
pk7272 Posted June 9, 2016 Author Share Posted June 9, 2016 1 minute ago, jchd said: Please post sample input (masquerade private data is any). Use of FileRead and an ad hoc regexp will surely do. Hi jchd, Please find attached sample log file , My unique id is "005P9272" and the String which i need to find after we find the unique id is "Auth response is as follow :" samplelog.txt Link to comment Share on other sites More sharing options...
jchd Posted June 9, 2016 Share Posted June 9, 2016 (edited) That is a sketch only : no error checking done and no guard against thread being interleaved (possibility of auth response of another thread being just below the unique ID searched). But at least it gives you the idea: Local $sFile = "samplelog.txt" Local $sUniqueId = "005P9272" Local $sSearch = "Auth response is as follow :" Local $sText = FileRead($sFile) Local $sFound = StringRegExpReplace($sText, "(?ims).*?" & $sUniqueId & ".*?" & $sSearch & "\N*\R(\N*).*", "$1") MsgBox(0, "Response found", $sFound) It could even be made a one liner if you put everything inline... Edited June 9, 2016 by jchd This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt) Link to comment Share on other sites More sharing options...
pk7272 Posted June 9, 2016 Author Share Posted June 9, 2016 1 minute ago, jchd said: That is a sketch only : no error checking done and no guard against thread being interleaved (possibility of auth response of another thread being just below the unique ID searched). But at least it gives you the idea: Local $sFile = "samplelog.txt" Local $sUniqueId = "005P9272" Local $sSearch = "Auth response is as follow :" Local $sText = FileRead($sFile) Local $sFound = StringRegExpReplace($sText, "(?ims).*?" & $sUniqueId & ".*?" & $sSearch & "\N*\R(\N*).*", "$1") MsgBox(0, "Response found", $sFound) Hi jchd, Thank you very much for your help (and nee to go and learn regex) ....just one thing , is this a generic because its not specific that $sSearch would be in the same place , i mean it might be a line upwards or downwards (very rare cases) Link to comment Share on other sites More sharing options...
jchd Posted June 9, 2016 Share Posted June 9, 2016 The regexp engine will do exactly what the pattern says, so its pretty much generic. Precise explanation given by https://regex101.com/ below. Now if ever the ID or the search string might contain regexp metacharacter, a good precaution would be to make sure these strings are interpreted literally. Also in this case, the m modifier (multiline) isn't needed. What do you mean by " a line upwards or downwards "? Is it that the identifier could be place after the Auth response??? In that case the pattern would need serious rewrite. OTOH if you mean that there can be an unspecified number of lines between ID and auth repsonse, then yes, the pattern is totally generic. Could be megabytes between id and response, but the first response found after ID will make it, regardless of thread. If you need something else, just ask. The code would then become: Local $sFile = "samplelog.txt" Local $sUniqueId = "005P9272" Local $sSearch = "Auth response is as follow :" Local $sText = FileRead($sFile) Local $sFound = StringRegExpReplace($sText, "(?is).*?\Q" & $sUniqueId & "\E.*?\Q" & $sSearch & "\E\N*\R(\N*).*", "$1") MsgBox(0, "Response found", $sFound) /(?is).*?005P9272.*?Auth response is as follow :\N*\R(\N*).*/ (?is) Match the remainder of the pattern with the following options: i modifier: insensitive. Case insensitive match (ignores case of [a-zA-Z]) s modifier: single line. Dot matches newline characters .*? matches any character Quantifier: *? Between zero and unlimited times, as few times as possible, expanding as needed [lazy] 005P9272 matches the characters 005P9272 literally (case insensitive) .*? matches any character Quantifier: *? Between zero and unlimited times, as few times as possible, expanding as needed [lazy] Auth response is as follow : matches the characters Auth response is as follow : literally (case insensitive) \N* matches any non-newline character Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy] \R matches any Unicode newline sequence; can be modified using verbs 1st Capturing group (\N*) \N* matches any non-newline character Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy] .* matches any character Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy] pk7272 1 This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt) Link to comment Share on other sites More sharing options...
Moderators JLogan3o13 Posted June 9, 2016 Moderators Share Posted June 9, 2016 Moved to appropriate forum "Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball How to get your question answered on this forum! Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now