Jump to content

Open a text file and remove blank lines?


 Share

Recommended Posts

This should work:

File:

Line1
Line2

Line3

Line4
Line5

Line6
Line7
Line8


Line9

Code:

#include <File.au3>
$Lines=_FileCountLines("text.txt")
Dim $ReadLines[$Lines]
Dim $NewLines[1]
$a=0
For $i=0 To $Lines-1
    $ReadLines[$i]=FileReadLine("text.txt",$i+1)
    If $ReadLines[$i]<>"" Then
        $NewLines[$a] = $ReadLines[$i]
        $a+=1
        ReDim $NewLines[$a+1]
    EndIf
Next
_FileWriteFromArray("text.txt",$NewLines)

File output:

Line1
Line2
Line3
Line4
Line5
Line6
Line7
Line8
Line9

It has a little problem though, the new written file has a carriage return at the end.

Link to comment
Share on other sites

  • Moderators

$sS = "I am a string" & @CRLF & @CRLF & _
    "With Empty Lines" & @CRLF & @CRLF & _
    "Please Remove those empty lines"
MsgBox(0, "Before", $sS)
$sS = _StringReplaceBlank($sS, 1)
MsgBox(0, "Replaced: " & @extended & " lines.", $sS)

Func _StringReplaceBlank($sString, $sSpaces = "")
    If $sSpaces Then $sSpaces = "\s*"
    $sString = StringRegExpReplace($sString, "(?s)\r\n" & $sSpaces & "\r\n", @CRLF)
    If @extended Then Return SetError(0, @extended, $sString)
    Return SetError(1, 0, $sString)
EndFunc

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

  • Moderators

Yeah, that one ^ is good. Much easier, I didn't think of that... <_<

I meant evilertoaster :)

His was the same concept so to speak as mine... I like the function type a bit more because I get how many lines were actually removed, and have the option to remove lines that just have spaces on them with no other data.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Try

$File = <path\file>
$tFile = @TempDir & "\Temp.txt"
$Array = ""
_FileRead2Array($File,$Array)
For $I = 1 To Ubound($Array)-1
  If $Array[$I] <> "" Then FileWriteLine($tFile, $Array[$I]
Next
FileCopy($tFile, $File, 1)
FileDelete($tFile)

EDIT: You have to #include <file.au3> for this.

Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

  • Moderators

Try

$File = <path\file>
$tFile = @TempDir & "\Temp.txt"
$Array = ""
_FileRead2Array($File,$Array)
For $I = 1 To Ubound($Array)-1
  If $Array[$I] <> "" Then FileWriteLine($tFile, $Array[$I]
Next
FileCopy($tFile, $File, 1)
FileDelete($tFile)

EDIT: You have to #include <file.au3> for this.

Why would you loop through every line, open the file every loop and write to it?

Two solutions were provided with one read of the file...

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Why would you loop through every line, open the file every loop and write to it?

Two solutions were provided with one read of the file...

1) Because once it's in an array it's much easier to do any other line manipulation that you may decide on later

2) Although RegExpr is very good there is a tendency toward over-use.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

  • Moderators

1) Because once it's in an array it's much easier to do any other line manipulation that you may decide on later

2) Although RegExpr is very good there is a tendency toward over-use.

I love arrays just as much as the next person (maybe more).

But... You have a simple task, to remove all carriage returns and line feeds that are together indicating blank line(s).

So these are the steps you are taking.

1. Read file

2. Strip Carriage Returns

3. Split at Line Feed

4. Start at line 1

5. Each line, check for a value

6. Open File/Write to File/Close File

7. Continue Loop

Now normally, if you were looking for specific strings on a specific line, I'd agree with all but step 6 there, I'd store the information in a variable with CRLF and have them concatenated on the end of each loop where necessary. Then I'd Open the file one time for a write. The speed you'll find (for larger files, or smaller, doesn't really matter) would be much greater then opening the file every loop for a write.

However, the OP simply asked how to remove blank lines... There were two proper solutions given IMHO.

1. StringReplace - Remove all CRLFCRLF <- indicating blank line(s)

2. StringRegExpReplace - Remove all CRLFCRLF (/r = carriage return, /n equals line feed) indicating blank lines, with an option (/s* indicating "possible" spaces only), so that if the line only contains spaces and no other chars, you have the option to remove that line as well.

Both options would be basically.

1. Read File

2. Replace Chars

3. Write to file

Cutting out looping is going to save many times the speed of the other suggestions (if speed were an issue), and address the issue that the OP had specifically.

... Done with my dissertation <_<

Edit:

Had to fix an ebonic moment, I'm sure there are others.

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

One of the examples given used FilReadLine which is the slowest of all methods to work with. That one should be avoided whenever pssible.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

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...