Jump to content

How to delete blank lines in txt file with StringRegExpReplace method


 Share

Recommended Posts

Friends,I can not delete blank lines in txt file

TXT.txt File

Line1
Line2


Line5


Line8

 

#include <array.au3>
#include <file.au3>
#include <String.au3>
#include <StringConstants.au3>

$Inputbox = InputBox("Enter", "Enter what you want to delete", "", "")
    If @error Then Exit

            If Not @error Then

Global $File_to_written = @ScriptDir & "\" & "TXT.txt"
Local $FileOpen = FileOpen($File_to_written, 2)
If $FileOpen = -1 Then Exit MsgBox(17, 'ERROR', 'File open failed for output file')

Local $ReadString = FileRead(@ScriptDir & "\OLD" & "\TXTOLD.txt")

Local $aTXTSPLIT = StringSplit($ReadString, @CRLF, 3)

For $1 = 0 To UBound($aTXTSPLIT) - 1
$ReadString = StringRegExpReplace($aTXTSPLIT[$1],$Inputbox, "")

;----------------------------These parts do not work---------
;$ReadString = StringRegExpReplace($ReadString, '(?s)[\n\r\t\v]', '')
;$ReadString = StringRegExpReplace($ReadString, "(?m:^)\h*(\r\n|\r|\n)", "")
;$ReadString = StringRegExpReplace($ReadString, '(?m:^\s*[\r\n])', '')
;$ReadString = StringRegExpReplace($ReadString, '(?s)\r\n', '')
;---------------------------These parts do not work------------

$ReadString = StringStripWS($ReadString,8)
FileWrite($FileOpen, $ReadString & @CRLF)
Next
FileClose($FileOpen)

            EndIf

The following codes are working.
But I want to do it with StringRegExpReplace

Global $aLines
_FileReadToArray($File_to_written, $aLines)
For $i = $aLines[0] To 1 Step -1
    If $aLines[$i] = "" Then
        _ArrayDelete($aLines, $i)
    EndIf
Next
_FileWriteFromArray($File_to_written, $aLines, 1)

 

Edited by youtuber
Link to comment
Share on other sites

StringReplace(StringReplace(StringReplace($sFile , @CRLF , "|") , "||" , "") , "|" , @CRLF))

?!

also with a flurry of array funcs?

#include<array.au3>
$arr = FileReadToArray("test.txt")
_ArrayDelete($arr , _ArrayToString(_ArrayFindAll($arr , "") , ";"))
_ArrayDisplay($arr)

 

Edited by iamtheky

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

or this one

msgbox(0, '' , StringReplace(StringStripCR(FileRead("txt.txt")) , @LF & @LF , ""))

 

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

@mikell Not clearing blank lines :(

#include <array.au3>
#include <file.au3>
#include <String.au3>
#include <StringConstants.au3>

$Inputbox = InputBox("Enter", "Enter what you want to delete", "", "")
If @error Then Exit
If Not @error Then
    Global $File_to_written = @ScriptDir & "\" & "TXT.txt"
    Local $FileOpen = FileOpen($File_to_written, 2)
    If $FileOpen = -1 Then Exit MsgBox(17, 'ERROR', 'File open failed for output file')
    Local $ReadString = FileRead(@ScriptDir & "\OLD" & "\TXTOLD.txt")
    Local $aTXTSPLIT = StringSplit($ReadString, @CRLF, 3)
    For $i = 0 To UBound($aTXTSPLIT) - 1
        $ReadString = StringRegExpReplace($aTXTSPLIT[$i], $Inputbox, "")
        $ReadString = StringRegExpReplace($ReadString, "(?m)^\s*$\R?", "")
        $ReadString = StringStripWS($ReadString, 8)
        FileWrite($FileOpen, $ReadString & @CRLF)
    Next
    FileClose($FileOpen)
EndIf

 

Link to comment
Share on other sites

Like this?

Local $s = @CRLF & "One" & @CRLF & @CRLF & "Two" & @CRLF & "Three" & @CRLF & @CRLF & _
           "Four" & @CRLF & "" & @CRLF & "Five" & @CRLF & "Six" & @CRLF & "" & @CRLF & "" & @CRLF

ConsoleWrite(StringRegExpReplace($s, "(?m)(\A\R)|(\R(?=\R|\z))", "") & @LF)

This regexp removes leading, extra, trailing line breaks.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Using mikell's SRE you could try this

$Inputbox = InputBox("Enter", "Enter what you want to delete", "", "")
If @error Then Exit

Local $s_FileRead = FileRead(@ScriptDir & "\OLD" & "\TXTOLD.txt")
Local $h_FileOpen = FileOpen(@ScriptDir & "\" & "TXT.txt", 2)
FileWrite($h_FileOpen, StringRegExpReplace(StringReplace($s_FileRead, $Inputbox, ''), "(?m)^\s*$\R?", ""))
FileClose($h_FileOpen)

 

Edited by benners
Forgot FileClose
Link to comment
Share on other sites

17 minutes ago, benners said:

Using mikell's SRE you could try this

$Inputbox = InputBox("Enter", "Enter what you want to delete", "", "")
If @error Then Exit

Local $s_FileRead = FileRead(@ScriptDir & "\OLD" & "\TXTOLD.txt")
Local $h_FileOpen = FileOpen(@ScriptDir & "\" & "TXT.txt", 2)
FileWrite($h_FileOpen, StringRegExpReplace(StringReplace($s_FileRead, $Inputbox, ''), "(?m)^\s*$\R?", ""))
FileClose($h_FileOpen)

 

Your example does not delete two white spaces

Sample txt

Line1
Line2
Line5
Line8
  line10
$Inputbox = InputBox("Enter", "Enter what you want to delete", "", "")
If @error Then Exit

Local $s_FileRead = FileRead(@ScriptDir & "\OLD" & "\TXTOLD.txt")
Local $h_FileOpen = FileOpen(@ScriptDir & "\" & "TXT.txt", 2)
FileWrite($h_FileOpen, StringRegExpReplace(StringReplace($s_FileRead, $Inputbox, ''), "(?m)^\s*$\R?", ""))
StringStripWS($s_FileRead, 8)
FileClose($h_FileOpen)

 

Link to comment
Share on other sites

21 minutes ago, mikell said:

In your pattern what is (?m) for ?

It's actually pointless, sorry. Copy/paste in a bit of hurry.

Now it seems the OP decides that leading (and trailing ?) whitespaces should also be removed. I give up following a moving target.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

another one :D

Local $s = @CRLF & "One" & @CRLF & @CRLF & "Two" & @CRLF & "Three" & @CRLF & @CRLF & _
           "Four" & @CRLF & "" & @CRLF & "Five" & @CRLF & "Six" & @CRLF & "" & @CRLF & "" & @CRLF

ConsoleWrite( StringRegExpReplace($s, "^\R+|\R+$|\R\K\R+", "") )

 

Link to comment
Share on other sites

2 hours ago, youtuber said:

Your example does not delete two white spaces

Tweaked for the last time

#include <StringConstants.au3>

Local $s_Delete = InputBox("Enter", "Enter what you want to delete")
If @error Then Exit

Local $h_FileOpen = FileOpen(@ScriptDir & "\TXT.txt", 2)
FileWrite($h_FileOpen, StringRegExpReplace(StringRegExpReplace(StringReplace(FileRead(@ScriptDir & "\OLD\TXTOLD.txt"), $s_Delete, ''), "(?m)^\s*$\R?", ""), '(?m)^\s+|\s+\Z', ''))
FileClose($h_FileOpen)

 

Link to comment
Share on other sites

3 hours ago, youtuber said:

our example does not delete two white spaces

Sample txt

Line1
Line2
Line5
Line8
  line10

This one also corrects the capitalization of 'line' while we are being all needy

ConsoleWrite(stringreplace(StringStripWS(StringReplace(StringStripCR(FileRead("txt.txt")) , @LF & @LF , @CRLF) , 4) , @LF & "l" , @LF & "L"))

 

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

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