Jump to content

Removing blank lines in a text file.


Recommended Posts

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

Link to comment
Share on other sites

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