chadskinner Posted July 18, 2012 Posted July 18, 2012 (edited) I have a bunch of log files I need to change about %30 of the line where a : was used instead of a coma. I can't just replace all the colons or the time/date stamp would get messed up. The kicker is there is roughly 15 different syntax's before and after the colon. Example Result1:Sub1:55.09 Result1:Sub2:63.10 NewResult1:105.10 NewResult2:110.20 I need to replace all those colons in about a 100+ files. So I've been trying stringregexreplace I find my files via filefindnext file turn each one into a string, replace the parameters, write it back to the file. So far I've Tried things like stringregexreplace($fileString,'(?!{6,9}d{1})((?!{3}d{1})',',') This one just replaces the entire string with a coma. I guess I don't understand what 'non-capturing' group means. So I tried it this way stringregexreplace($fileString,'?!{6,9}d{1}(?!{3}d{1}',',') again replaces the entire string. Does anyone know if it's possible to just replace a substring inside a pattern match and not destroy the rest of this. I think I could write wayyyy too much code and do this with StringRegEx and a lot of loops but it seems like StringRegExReplace should be able to do this. simple example $fileOpen = FileOpen($dir & 'my.log',0) $fileRead = FileRead($fileOpen) fileclose($fileOpen) $fileString = String($fileRead) MsgBox(1,'FileString', $fileString) $newString = StringRegExpReplace($fileString,',[A-Za-z]{3}[0-9]{1}(',',') MsgBox(1,'NewString', @error & $newString ) Any thoughtsreferences would be greatly appreciated. Thanks, Chad Edited July 18, 2012 by chadskinner
chadskinner Posted July 18, 2012 Author Posted July 18, 2012 BTW - each of the @$@##$$ smiley faces is actually comma close parens but I'm too stupid to figure out how to get this editor to stop doing that.
UEZ Posted July 18, 2012 Posted July 18, 2012 Can you give another example how your log file looks like incl. the time and how the result should be? Thanks, UEZ Please don't send me any personal message and ask for support! I will not reply! Selection of finest graphical examples at Codepen.io The own fart smells best! ✌Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!¯\_(ツ)_/¯ ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ
AZJIO Posted July 18, 2012 Posted July 18, 2012 ; $fileString = FileRead($dir & 'my.log') $fileString = _ 'Result1:Sub1:55.09' & @CRLF & _ 'Result1:Sub2:63.10' $newString = StringRegExpReplace($fileString, ':[A-Za-z]{3}d', '') MsgBox(0, 'Yes?', @error & @CRLF & $newString) My other projects or all
chadskinner Posted July 18, 2012 Author Posted July 18, 2012 Entry has colons separating values instead of comas INFO,2012-05-23 15:03:58,934,Result1:Reading1:94.75 INFO,2012-05-23 15:03:58,934,Result1:Reading2:94.75 INFO,2012-05-23 15:03:59,276,Result2:Reading1:81.21 INFO,2012-05-23 15:03:59,276,Result2:Reading2:81.21 INFO,2012-05-23 15:03:59,464,NewResult1:61.58 INFO,2012-05-23 15:03:59,558,NewResult2:55.67 INFO,2012-05-23 15:03:59,651,NewResult3:59.13 INFO,2012-05-23 15:03:59,744,NewResult4:69.21 INFO,2012-05-23 15:03:59,869,NewResult5:62.32 INFO,2012-05-23 15:03:59,947,NewResult6:60.78 INFO,2012-05-23 15:04:00,088,NewResult7:59.16 INFO,2012-05-23 15:04:00,259,NewResult8:57.67 INFO,2012-05-23 15:04:00,322,NewResult9:59.28 INFO,2012-05-23 15:04:00,384,NewResult10:55.65 Part of the log file that is correct INFO,2012-05-23 15:05:42,658,Result[0]V,1.11 INFO,2012-05-23 15:05:44,592,Result[1]V,1.12 INFO,2012-05-23 15:05:46,559,V1,1.11 INFO,2012-05-23 15:05:48,509,V2,0.00 INFO,2012-05-23 15:05:50,459,sysV,1.11 INFO,2012-05-23 15:05:52,378,sys2V,1.34 INFO,2012-05-23 15:05:54,312,sys3V,1.32 INFO,2012-05-23 15:05:56,246,sys4V,1.32 INFO,2012-05-23 15:05:58,243,sys5V,1.51 INFO,2012-05-23 15:05:58,539,main,77.50 How the bad section should look (note time stamp is preserved but the rest of the colons are gone) INFO,2012-05-23 15:03:58,934,Result1,Reading1,94.75 INFO,2012-05-23 15:03:58,934,Result1,Reading2,94.75 INFO,2012-05-23 15:03:59,276,Result2,Reading1,81.21 INFO,2012-05-23 15:03:59,276,Result2,Reading2,81.21 INFO,2012-05-23 15:03:59,464,NewResult1,61.58 INFO,2012-05-23 15:03:59,558,NewResult2,55.67 INFO,2012-05-23 15:03:59,651,NewResult3,59.13 INFO,2012-05-23 15:03:59,744,NewResult4,69.21 INFO,2012-05-23 15:03:59,869,NewResult5,62.32 INFO,2012-05-23 15:03:59,947,NewResult6,60.78 INFO,2012-05-23 15:04:00,088,NewResult7,59.16 INFO,2012-05-23 15:04:00,259,NewResult8,57.67 INFO,2012-05-23 15:04:00,322,NewResult9,59.28 INFO,2012-05-23 15:04:00,384,NewResult10,55.65 My plan was to read the file into a string, parse the string with regexreplace and write it back to the file. I have 100's of these improperly formatted files.
BrewManNH Posted July 19, 2012 Posted July 19, 2012 Try this on one of your text files and see if it does what you're looking for. #include <file.au3> #include <_array.au3> Global $aFile _FileReadToArray("test.txt", $aFile) For $I = 1 To $aFile[0] $aFile[$I] = StringRegExpReplace($aFile[$I], "Result(d+):", "Result$1,") $aFile[$I] = StringRegExpReplace($aFile[$I], "Reading(d+):", "Reading$1,") Next _ArrayDisplay($aFile) I'm just learning RegEx so this could probably be done in one line instead of 2, but I have no idea how to do it that way yet. Also, change the text file name in the _FileReadToArray line to one of your filenames, with path if not in the same folder as the script. If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag GudeHow to ask questions the smart way! I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from. Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays. - ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script. - Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label. - _FileGetProperty - Retrieve the properties of a file - SciTE Toolbar - A toolbar demo for use with the SciTE editor - GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI. - Latin Square password generator
BrewManNH Posted July 19, 2012 Posted July 19, 2012 I gave it some more thought and came up with a little bit better RegEx, probably still not as good as it could be, but I'm learning. muttley #include <file.au3> #include <_array.au3> Global $aFile _FileReadToArray("test.txt", $aFile) For $I = 1 To $aFile[0] $aFile[$I] = StringRegExpReplace($aFile[$I], "([a-zA-Z]{6})(d+):", "$1$2,") Next _ArrayDisplay($aFile) This pattern will match any string of 6 letters followed by any number of digits and then a colon. So, it matches the "Result##:" and "Reading##:" strings and replaces the colons with commas accordingly. If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag GudeHow to ask questions the smart way! I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from. Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays. - ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script. - Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label. - _FileGetProperty - Retrieve the properties of a file - SciTE Toolbar - A toolbar demo for use with the SciTE editor - GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI. - Latin Square password generator
Malkey Posted July 19, 2012 Posted July 19, 2012 This example shows the uselessness of arrays. ; If any of the three characters before the ":" are a space or a colon (:), then do not replace that colon with a coma. $sSring = StringRegExpReplace(FileRead("test.txt"), "([^: ]{3}):", "$1,") ConsoleWrite($sSring & @LF)
chadskinner Posted July 19, 2012 Author Posted July 19, 2012 Holy crap, I owe you a beer sir. You taught me more in that one line than I thought possible. Elegant job, Thanks!!
BrewManNH Posted July 19, 2012 Posted July 19, 2012 This example shows the uselessness of arrays. Not sure how it demonstrates anything about the utility of using arrays, but a very nice approach to the situation nonetheless. If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag GudeHow to ask questions the smart way! I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from. Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays. - ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script. - Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label. - _FileGetProperty - Retrieve the properties of a file - SciTE Toolbar - A toolbar demo for use with the SciTE editor - GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI. - Latin Square password generator
UEZ Posted July 19, 2012 Posted July 19, 2012 Here another way: $sText = _ "INFO,2012-05-23 15:03:58,934,Result1:Reading1:94.75" & @CRLF & _ "INFO,2012-05-23 15:03:58,934,Result1:Reading2:94.75" & @CRLF & _ "INFO,2012-05-23 15:03:59,276,Result2:Reading1:81.21" & @CRLF & _ "INFO,2012-05-23 15:03:59,276,Result2:Reading2:81.21" & @CRLF & _ "INFO,2012-05-23 15:03:59,464,NewResult1:61.58" & @CRLF & _ "INFO,2012-05-23 15:03:59,558,NewResult2:55.67" & @CRLF & _ "INFO,2012-05-23 15:03:59,651,NewResult3:59.13" & @CRLF & _ "INFO,2012-05-23 15:03:59,744,NewResult4:69.21" & @CRLF & _ "INFO,2012-05-23 15:03:59,869,NewResult5:62.32" & @CRLF & _ "INFO,2012-05-23 15:03:59,947,NewResult6:60.78" & @CRLF & _ "INFO,2012-05-23 15:04:00,088,NewResult7:59.16" & @CRLF & _ "INFO,2012-05-23 15:04:00,259,NewResult8:57.67" & @CRLF & _ "INFO,2012-05-23 15:04:00,322,NewResult9:59.28" & @CRLF & _ "INFO,2012-05-23 15:04:00,384,NewResult10:55.65" & @CRLF & _ "INFO,2012-05-23 15:05:42,658,Result[0]V,1.11" & @CRLF & _ "INFO,2012-05-23 15:05:44,592,Result[1]V,1.12" & @CRLF & _ "INFO,2012-05-23 15:05:46,559,V1,1.11" & @CRLF & _ "INFO,2012-05-23 15:05:48,509,V2,0.00" & @CRLF & _ "INFO,2012-05-23 15:05:50,459,sysV,1.11" & @CRLF & _ "INFO,2012-05-23 15:05:52,378,sys2V,1.34" & @CRLF & _ "INFO,2012-05-23 15:05:54,312,sys3V,1.32" & @CRLF & _ "INFO,2012-05-23 15:05:56,246,sys4V,1.32" & @CRLF & _ "INFO,2012-05-23 15:05:58,243,sys5V,1.51" & @CRLF & _ "INFO,2012-05-23 15:05:58,539,main,77.50" $sNew = StringRegExpReplace($sText & @CRLF, "(.*)(:)(d*.d*rn)", "$1,$3") MsgBox(0, "Test", $sNew) Br, UEZ Please don't send me any personal message and ask for support! I will not reply! Selection of finest graphical examples at Codepen.io The own fart smells best! ✌Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!¯\_(ツ)_/¯ ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ
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