willichan Posted June 6, 2014 Posted June 6, 2014 I am baffled as to why I can't make such a simple thing work. I have a CSV file that has CHR(10) linefeed characters in multi-line fields. To ease handling of this file by an intermediate process, I am trying to replace the linefeed characters with the string "[[LF]]". I have tried three different ways to replace the character, but none of them seems to do anything. My test code looks like this: #include <FileConstants.au3> $file = FileOpen("test.csv", $FO_READ) $data = FileRead($file) FileClose($file) StringReplace($data, @LF, "[[LF]]", 0) StringReplace($data, Chr(10), "[[LF]]", 0) StringReplace($data, ChrW(10), "[[LF]]", 0) $file = FileOpen("test2.csv", $FO_OVERWRITE) FileWrite($file, $data) FileClose($file) When I look at the test2.csv file, it still has the linefeed characters, and no "[[LF]]" strings. Am I just brain dead here, or what is wrong? Reproducer data file would look something like this (<CR> and <LF> represend the characters, not literal text): "First","Last","Nickname","Photo"<CR><LF> "John","Smith","Jonny<LF> Mr. Smith","john_smith.jpg"<CR><LF> "Michael","Small","Little Mike","michael_small.jpg"<CR><LF> "Jonathan","Walker","Johnnie Walker<LF> Town Drunk<LF> I'll have another","jonathan_walker.jpg"<CR><LF> I am using the latest AutoIt v3.3.12.0. My UDFs: Barcode Libraries, Automate creation of any type of project folder, File Locking with Cooperative Semaphores, Inline binary files, Continue script after reboot, WinWaitMulti, Name Aggregator, Enigma, CornedBeef Hash
UEZ Posted June 6, 2014 Posted June 6, 2014 Can you please upload the example txt? 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!¯\_(ツ)_/¯ ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ
Gianni Posted June 6, 2014 Posted June 6, 2014 hi willichan you forgot to catch the result of the StrngReplace() function just put a new variable in front of the function or just the $data variable itself: $data = StringReplace($data, @LF, "[[LF]]", 0) Chimp small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....
Solution jchd Posted June 7, 2014 Solution Posted June 7, 2014 willichan, Chimp just pointed out why no replacement took place, but this is not the end of the day. By replacing every @LF your CSV lines will now be terminated by @CR only followed by [[LF]] as the start of the next line, which is not what you want. Instead you need to replace @LF which are not preceded by @CR and a regular expression is better suited for the task: Local $csv = '"First","Last","Nickname","Photo"' & @CRLF & _ '"John","Smith","Jonny' & @LF & 'Mr. Smith","john_smith.jpg"' & @CRLF & _ '"Michael","Small","Little Mike","michael_small.jpg"' & @CRLF & _ '"Jonathan","Walker","Johnnie Walker' & @LF & 'Town Drunk' & @LF & 'I''ll have another","jonathan_walker.jpg"' & @CRLF ConsoleWrite(StringRegExpReplace($csv, '(?<!\r)\n', '[[LF]]')) 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)
Belini Posted June 8, 2014 Posted June 8, 2014 (edited) @willichan try this: $test = FileRead("test.csv") $nText = StringRegExpReplace($test,'(?i)\@LF\b','[[LF]]') MsgBox(4096,"Result",$nText) or replace the file directly. #include <File.au3> $nText = _ReplaceStringInFile("test.csv", "@LF", "[[LF]]") Edited June 8, 2014 by Belini My Codes: Virtual Key Code UDF: http://www.autoitscript.com/forum/topic/138246-virtual-key-code-udf/ GuiSplashTextOn.au3: http://www.autoitscript.com/forum/topic/143542-guisplashtexton-udf/ Menu versions of Autoit: http://www.autoitscript.com/forum/topic/137435-menu-versions-of-autoit/#entry962011 Selects first folder of letters: ]http://www.autoitscript.com/forum/topic/144780-select-folders-by-letter/#entry1021708/spoiler] List files and folders with long addresses.: http://www.autoitscript.com/forum/topic/144910-list-files-and-folders-with-long-addresses/#entry102 2926 Program JUKEBOX made in Autoit:some functions:http://www.youtube.com/watch?v=WJ2tC2fD5Qs Navigation to search:http://www.youtube.com/watch?v=lblwOFIbgtQ
jchd Posted June 8, 2014 Posted June 8, 2014 Belini, That doesn't solve the issue I raised. 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)
Belini Posted June 8, 2014 Posted June 8, 2014 Belini, That doesn't solve the issue I raised. The first option does not work anymore substituting directly in the works Test: https://www.dropbox.com/s/ckasvauah0i3apy/Test_Replace.rar My Codes: Virtual Key Code UDF: http://www.autoitscript.com/forum/topic/138246-virtual-key-code-udf/ GuiSplashTextOn.au3: http://www.autoitscript.com/forum/topic/143542-guisplashtexton-udf/ Menu versions of Autoit: http://www.autoitscript.com/forum/topic/137435-menu-versions-of-autoit/#entry962011 Selects first folder of letters: ]http://www.autoitscript.com/forum/topic/144780-select-folders-by-letter/#entry1021708/spoiler] List files and folders with long addresses.: http://www.autoitscript.com/forum/topic/144910-list-files-and-folders-with-long-addresses/#entry102 2926 Program JUKEBOX made in Autoit:some functions:http://www.youtube.com/watch?v=WJ2tC2fD5Qs Navigation to search:http://www.youtube.com/watch?v=lblwOFIbgtQ
jchd Posted June 8, 2014 Posted June 8, 2014 (edited) Now I see! You're joking, right? BTW, next time please simply post code inline inside AutoIt codebox. Edited June 8, 2014 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)
Belini Posted June 9, 2014 Posted June 9, 2014 (edited) I kidding? I had already posted inside the codebox AutoIt in post number 5. Test result using _ReplaceStringInFile, Not was replaced @CRLF. '"First","Last","Nickname","Photo"' & @CRLF & _ '"John","Smith","Jonny' & [[LF]] & 'Mr. Smith","john_smith.jpg"' & @CRLF & _ '"Michael","Small","Little Mike","michael_small.jpg"' & @CRLF & _ '"Jonathan","Walker","Johnnie Walker' & [[LF]] & 'Town Drunk' & [[LF]] & 'I''ll have another","jonathan_walker.jpg"' & @CRLF Edited June 9, 2014 by Belini My Codes: Virtual Key Code UDF: http://www.autoitscript.com/forum/topic/138246-virtual-key-code-udf/ GuiSplashTextOn.au3: http://www.autoitscript.com/forum/topic/143542-guisplashtexton-udf/ Menu versions of Autoit: http://www.autoitscript.com/forum/topic/137435-menu-versions-of-autoit/#entry962011 Selects first folder of letters: ]http://www.autoitscript.com/forum/topic/144780-select-folders-by-letter/#entry1021708/spoiler] List files and folders with long addresses.: http://www.autoitscript.com/forum/topic/144910-list-files-and-folders-with-long-addresses/#entry102 2926 Program JUKEBOX made in Autoit:some functions:http://www.youtube.com/watch?v=WJ2tC2fD5Qs Navigation to search:http://www.youtube.com/watch?v=lblwOFIbgtQ
jchd Posted June 9, 2014 Posted June 9, 2014 Once again realize that what you're doing is replacing the string @LF (those 3 characters litterally) in an AutoIt source code snippet mimicking the content of said CSV file. This has nothing to do with the problem exposed by the OP, which is replacing lone linefeed characters (i.e. not preceeded by a carriage return character) by the string [[LF]] in actual CSV file. 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)
Belini Posted June 9, 2014 Posted June 9, 2014 So sorry, just tried to help. My Codes: Virtual Key Code UDF: http://www.autoitscript.com/forum/topic/138246-virtual-key-code-udf/ GuiSplashTextOn.au3: http://www.autoitscript.com/forum/topic/143542-guisplashtexton-udf/ Menu versions of Autoit: http://www.autoitscript.com/forum/topic/137435-menu-versions-of-autoit/#entry962011 Selects first folder of letters: ]http://www.autoitscript.com/forum/topic/144780-select-folders-by-letter/#entry1021708/spoiler] List files and folders with long addresses.: http://www.autoitscript.com/forum/topic/144910-list-files-and-folders-with-long-addresses/#entry102 2926 Program JUKEBOX made in Autoit:some functions:http://www.youtube.com/watch?v=WJ2tC2fD5Qs Navigation to search:http://www.youtube.com/watch?v=lblwOFIbgtQ
jchd Posted June 9, 2014 Posted June 9, 2014 Glad you see the light, no harm done. I provided the help needed in >my previous post. 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)
willichan Posted June 12, 2014 Author Posted June 12, 2014 Thanks to all. Chimp identified where my brain lag fell. jchd, thanks for the regex code. That did the trick nicely. My UDFs: Barcode Libraries, Automate creation of any type of project folder, File Locking with Cooperative Semaphores, Inline binary files, Continue script after reboot, WinWaitMulti, Name Aggregator, Enigma, CornedBeef Hash
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