Sign in to follow this  
Followers 0
willichan

StringReplace LF character

13 posts in this topic

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.

Share this post


Link to post
Share on other sites



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!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites

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)

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Share this post


Link to post
Share on other sites

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 here
RegExp tutorial: enough to get started
PCRE 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)

Share this post


Link to post
Share on other sites

#5 ·  Posted (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 by Belini

Share this post


Link to post
Share on other sites

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 here
RegExp tutorial: enough to get started
PCRE 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)

Share this post


Link to post
Share on other sites

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Now I see! You're joking, right?

BTW, next time please simply post code inline inside AutoIt codebox.

Edited 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 here
RegExp tutorial: enough to get started
PCRE 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)

Share this post


Link to post
Share on other sites

#9 ·  Posted (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 by Belini

Share this post


Link to post
Share on other sites

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 here
RegExp tutorial: enough to get started
PCRE 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)

Share this post


Link to post
Share on other sites

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 here
RegExp tutorial: enough to get started
PCRE 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)

Share this post


Link to post
Share on other sites

Thanks to all.

Chimp identified where my brain lag fell.

jchd, thanks for the regex code.  That did the trick nicely.

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0