Jump to content
Sign in to follow this  
jebus495

Removing blank lines in a text file.

Recommended Posts

jebus495

#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
Malkey

#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
jebus495

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.