Sign in to follow this  
Followers 0
jebus495

Removing blank lines in a text file.

4 posts in this topic

#include <file.au3>
#include <Array.au3>

$lines = _FileCountLines("prepatch.log")
$x2 = 1
For $x2 = 1 To $lines
$linestring = FileReadLine("prepatch.log", $x2)
$split = StringSplit($linestring, " ")

For $x = 1 to $split[0]
    _FileWriteToLine("wordlist2.txt",$x2, $split[$x], 1)
Next
Next

The above will read a text file and put every word on a separate line. It also creates a lot of blank lines in the process so now I need a way to remove blank lines or prevent them in the first place.

Dim $aLines
Dim $sFile = "wordlist2.txt"
_FileReadToArray($sFile, $aLines)
_ArrayDelete($aLines,4)
FileDelete($sFile)
For $i=1 to $aLines[0]
    If $aLines[$i]<>"" Then FileWriteLine($sFile,$aLines[$i])
Next

I found this from searching but I can't seem to get it to work properly...

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

This works, Reads the file then changes it to hex

removes the lines that are blank, then changes it back from hex.

#include <String.au3>

$File = FileRead("File.txt")
$FileOut = _StringtoHex($File)
$FileEdit = StringReplace($FileOut, "0D0A0D0A", "0D0A")
ConsoleWrite(_HexToString($FileEdit)&@CRLF)
Edited by BuckMaster

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

#include <file.au3>
#include <Array.au3>

$lines = _FileCountLines("prepatch.log")
$x2 = 1
For $x2 = 1 To $lines
$linestring = FileReadLine("prepatch.log", $x2)
$split = StringSplit($linestring, " ")

For $x = 1 to $split[0]
    _FileWriteToLine("wordlist2.txt",$x2, $split[$x], 1)
Next
Next

The above will read a text file and put every word on a separate line. It also creates a lot of blank lines in the process so now I need a way to remove blank lines or prevent them in the first place.

Dim $aLines
Dim $sFile = "wordlist2.txt"
_FileReadToArray($sFile, $aLines)
_ArrayDelete($aLines,4)
FileDelete($sFile)
For $i=1 to $aLines[0]
    If $aLines[$i]<>"" Then FileWriteLine($sFile,$aLines[$i])
Next

I found this from searching but I can't seem to get it to work properly...

This appears to work.

;
#include <file.au3>
#include <array.au3>
Local $sFileOut = "wordlist2.txt"
Local $hFileOut = FileOpen($sFileOut, 9)

Local $sFile = "C:\PROGRA~1\AutoIt3\au3check.dat"
Local $sREResult = StringRegExp(FileRead($sFile), "\S+(?= |\v+|$)", 3) ; returns array of every word
;_ArrayDisplay($sREResult)

For $x = 0 To UBound($sREResult) - 1
    FileWriteLine($sFileOut, $sREResult[$x] & @CRLF)
Next
FileClose($hFileOut)
ShellExecute($sFileOut)
;

This is faster.

;
#include <file.au3>
#include <array.au3>
Local $sFileOut = "wordlist2.txt"
Local $hFileOut = FileOpen($sFileOut, 10)

Local $sFile = "C:\PROGRA~1\AutoIt3\au3check.dat"
Local $sREResult = StringRegExpReplace(FileRead($sFile), "(\S+)(\s*|\v+|$)", "\1" & @CRLF) ; returns string of every word to a line.
;ConsoleWrite($sREResult & @CRLF)

FileWrite($sFileOut, $sREResult)

FileClose($hFileOut)
ShellExecute($sFileOut)
;
Edited by Malkey

Share this post


Link to post
Share on other sites

Thank you Malkey!

That is much better than what I had lol.

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