Sign in to follow this  
Followers 0
leuce

Doing find/replace in an entire text file

4 posts in this topic

G'day everyone

I have hundreds of text files with superfluous blank lines and double spaces in them. I'd like to replace all double spaces with a single space (this may require several passes) and all double blank lines with a single blank line (again, several passes may be necessary). How would I do this?

This doesn't work:

$foo = FileOpen ("foo.txt", 128)
StringReplace ($foo, @CRLF & @CRLF, @CRLF)
FileClose ($foo)

I suppose I could do FileReadLine + SomeRegularExpression + FileWriteLine on every line of every file, but I suspect that is going to take a long, long time. I could also have the script open each file in Notepad (or Notepad++), perform the operation via the text editor, and save again, but then I can't do all of this in the background.

Is there any other way to perform such a whole-file operation?

Thanks

Samuel

Share this post


Link to post
Share on other sites



I have hundreds of text files with superfluous blank lines and double spaces in them. I'd like to replace all double spaces with a single space (this may require several passes) and all double blank lines with a single blank line (again, several passes may be necessary). How would I do this?

Hmm, the AutoIt help file says "FileWrite" only writes a single line, and I guess that comment did it for me. But if you think about it... a single line that contains several hard returns is still a single line :-)

This does work:

$foo = FileOpen ("foo.txt", 128)
$bar = FileRead ($foo)
$content = StringReplace ($bar, @CRLF & @CRLF, @CRLF)
FileClose ($foo)
$foo2 = FileOpen ("foo.txt", 130)
FileWrite ($foo2, $content)
FileClose ($foo2)oÝ÷ Ø   âqëh¹Èfk&Þ¶u§ÞÂX§zËajÙæiÉ.¥Ê)Êߢq¶ØhºZºÚ"µÍÌÍÙ[HH  ][ÝÙÛË  ][ÝÂÌÍÙÛÈH[SÜ[
    ÌÍÙ[KL
BÌÍØH[TXY
    ÌÍÙÛÊBÌÍÙÛØXÚÈH[SÜ[
    ÌÍÙ[H    [È ][Ý×ØXÚÝ   ][ÝËLÌ
B[UÜ]H
    ÌÍÙÛØXÚË ÌÍØBÌÍØÛÛ[HÝ[ÔXÙH
    ÌÍØÔ    [ÈÔÔB[PÛÜÙH
    ÌÍÙÛÊBÌÍÙÛÌH[SÜ[
    ][ÝÙÛË  ][ÝËLÌ
B[UÜ]H
    ÌÍÙÛÌ  ÌÍØÛÛ[
B[PÛÜÙH
    ÌÍÙÛÌ

Share this post


Link to post
Share on other sites

1. Read all the file names into an array by using "_FilelisttoArray"

2. Create a loop to go thru all of the files one by one and then use "_Filereadtoarray"

3. Create another loop to replace all the spaces and carriage returns in the file.

4. Write the new lines to a new file.

Share this post


Link to post
Share on other sites

The StringStripWS() function has an option to strip out double whitespace characters

It considers a bunch of stuff white space include @CR and @LF and spaces

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