youtuber Posted January 17, 2018 Share Posted January 17, 2018 (edited) 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 January 17, 2018 by youtuber Link to comment Share on other sites More sharing options...
benners Posted January 17, 2018 Share Posted January 17, 2018 Why not just check if $ReadString has any text when the whitespace has been stripped. $ReadString = StringStripWS($ReadString, 8) if $ReadString then FileWrite($FileOpen, $ReadString & @CRLF) youtuber 1 Link to comment Share on other sites More sharing options...
youtuber Posted January 17, 2018 Author Share Posted January 17, 2018 (edited) Fantastic,but how can this be so interesting StringStripWS ($ReadString, 8) is not just for deleting white blanks? Edited January 17, 2018 by youtuber Link to comment Share on other sites More sharing options...
benners Posted January 17, 2018 Share Posted January 17, 2018 The flag you used with StringStripWS will remove all spaces in the text. This works OK for the example you posted but if the line you want to keep has spaces between the words like, "I have spaces", it will become "Ihavespaces" when written to the file youtuber 1 Link to comment Share on other sites More sharing options...
mikell Posted January 17, 2018 Share Posted January 17, 2018 StringRegExpReplace($txt, "(?m)^\s*$\R?", "") ? youtuber and benners 2 Link to comment Share on other sites More sharing options...
iamtheky Posted January 17, 2018 Share Posted January 17, 2018 (edited) 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 January 18, 2018 by iamtheky youtuber 1 ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__) Link to comment Share on other sites More sharing options...
iamtheky Posted January 18, 2018 Share Posted January 18, 2018 or this one msgbox(0, '' , StringReplace(StringStripCR(FileRead("txt.txt")) , @LF & @LF , "")) youtuber 1 ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__) Link to comment Share on other sites More sharing options...
youtuber Posted January 18, 2018 Author Share Posted January 18, 2018 @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 More sharing options...
mikell Posted January 18, 2018 Share Posted January 18, 2018 Of course... your code splits the text to an array, and a StringRegExpReplace (or else) will not delete the array elements containing blank/empty lines Just FileRead the txt, apply the SRER to the whole string without splitting, and FileWrite the result youtuber 1 Link to comment Share on other sites More sharing options...
jchd Posted January 18, 2018 Share Posted January 18, 2018 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. youtuber 1 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 hereRegExp tutorial: enough to get startedPCRE 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 More sharing options...
benners Posted January 18, 2018 Share Posted January 18, 2018 (edited) 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 January 18, 2018 by benners Forgot FileClose youtuber 1 Link to comment Share on other sites More sharing options...
mikell Posted January 18, 2018 Share Posted January 18, 2018 @jchd In your pattern what is (?m) for ? Link to comment Share on other sites More sharing options...
youtuber Posted January 18, 2018 Author Share Posted January 18, 2018 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 More sharing options...
jchd Posted January 18, 2018 Share Posted January 18, 2018 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 hereRegExp tutorial: enough to get startedPCRE 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 More sharing options...
jguinch Posted January 18, 2018 Share Posted January 18, 2018 another one 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+", "") ) youtuber 1 Spoiler Network configuration UDF, _DirGetSizeByExtension, _UninstallList Firefox ConfigurationArray multi-dimensions, Printer Management UDF Link to comment Share on other sites More sharing options...
benners Posted January 18, 2018 Share Posted January 18, 2018 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) youtuber 1 Link to comment Share on other sites More sharing options...
iamtheky Posted January 18, 2018 Share Posted January 18, 2018 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")) youtuber 1 ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__) Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now