Jump to content

StringReplace LF character


Go to solution Solved by jchd,

Recommended Posts

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.

Link to comment
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)

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

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

Link to comment
Share on other sites

  • Solution

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)

Link to comment
Share on other sites

@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
Link to comment
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)

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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
Link to comment
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)

Link to comment
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)

Link to comment
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.

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...