sensalim Posted October 30, 2007 Share Posted October 30, 2007 I know I'm requesting a mini code here but I'm stuck. I have a file with lots of blank lines and I want them removed. Example: Line1 Line2 Line3 Becomes: Line1 Line2 Line3 Thanks. Link to comment Share on other sites More sharing options...
Nahuel Posted October 30, 2007 Share Posted October 30, 2007 This should work: File: Line1 Line2 Line3 Line4 Line5 Line6 Line7 Line8 Line9 Code: #include <File.au3> $Lines=_FileCountLines("text.txt") Dim $ReadLines[$Lines] Dim $NewLines[1] $a=0 For $i=0 To $Lines-1 $ReadLines[$i]=FileReadLine("text.txt",$i+1) If $ReadLines[$i]<>"" Then $NewLines[$a] = $ReadLines[$i] $a+=1 ReDim $NewLines[$a+1] EndIf Next _FileWriteFromArray("text.txt",$NewLines) File output: Line1 Line2 Line3 Line4 Line5 Line6 Line7 Line8 Line9 It has a little problem though, the new written file has a carriage return at the end. Link to comment Share on other sites More sharing options...
evilertoaster Posted October 30, 2007 Share Posted October 30, 2007 can do sth like this- $read=FileRead(@ScriptDir&"\test.txt") Do $read=StringReplace($read,@CRLF&@CRLF,@CRLF) Until @extended=0 FileWrite(@ScriptDir&"\testOut.txt",$read) Where text.txt is the file with the extra spaces you want out. Link to comment Share on other sites More sharing options...
Moderators SmOke_N Posted October 30, 2007 Moderators Share Posted October 30, 2007 $sS = "I am a string" & @CRLF & @CRLF & _ "With Empty Lines" & @CRLF & @CRLF & _ "Please Remove those empty lines" MsgBox(0, "Before", $sS) $sS = _StringReplaceBlank($sS, 1) MsgBox(0, "Replaced: " & @extended & " lines.", $sS) Func _StringReplaceBlank($sString, $sSpaces = "") If $sSpaces Then $sSpaces = "\s*" $sString = StringRegExpReplace($sString, "(?s)\r\n" & $sSpaces & "\r\n", @CRLF) If @extended Then Return SetError(0, @extended, $sString) Return SetError(1, 0, $sString) EndFunc Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer. Link to comment Share on other sites More sharing options...
Nahuel Posted October 30, 2007 Share Posted October 30, 2007 (edited) Yeah, that one ^ is good. Much easier, I didn't think of that... I meant evilertoaster Edited October 30, 2007 by Nahuel Link to comment Share on other sites More sharing options...
Moderators SmOke_N Posted October 31, 2007 Moderators Share Posted October 31, 2007 Yeah, that one ^ is good. Much easier, I didn't think of that... I meant evilertoaster His was the same concept so to speak as mine... I like the function type a bit more because I get how many lines were actually removed, and have the option to remove lines that just have spaces on them with no other data. Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer. Link to comment Share on other sites More sharing options...
GEOSoft Posted October 31, 2007 Share Posted October 31, 2007 (edited) Try $File = <path\file> $tFile = @TempDir & "\Temp.txt" $Array = "" _FileRead2Array($File,$Array) For $I = 1 To Ubound($Array)-1 If $Array[$I] <> "" Then FileWriteLine($tFile, $Array[$I] Next FileCopy($tFile, $File, 1) FileDelete($tFile) EDIT: You have to #include <file.au3> for this. Edited October 31, 2007 by GEOSoft George Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.*** The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number. Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else. "Old age and treachery will always overcome youth and skill!" Link to comment Share on other sites More sharing options...
Moderators SmOke_N Posted November 1, 2007 Moderators Share Posted November 1, 2007 Try $File = <path\file> $tFile = @TempDir & "\Temp.txt" $Array = "" _FileRead2Array($File,$Array) For $I = 1 To Ubound($Array)-1 If $Array[$I] <> "" Then FileWriteLine($tFile, $Array[$I] Next FileCopy($tFile, $File, 1) FileDelete($tFile) EDIT: You have to #include <file.au3> for this.Why would you loop through every line, open the file every loop and write to it? Two solutions were provided with one read of the file... Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer. Link to comment Share on other sites More sharing options...
GEOSoft Posted November 1, 2007 Share Posted November 1, 2007 Why would you loop through every line, open the file every loop and write to it?Two solutions were provided with one read of the file...1) Because once it's in an array it's much easier to do any other line manipulation that you may decide on later2) Although RegExpr is very good there is a tendency toward over-use. George Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.*** The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number. Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else. "Old age and treachery will always overcome youth and skill!" Link to comment Share on other sites More sharing options...
Moderators SmOke_N Posted November 1, 2007 Moderators Share Posted November 1, 2007 (edited) 1) Because once it's in an array it's much easier to do any other line manipulation that you may decide on later2) Although RegExpr is very good there is a tendency toward over-use.I love arrays just as much as the next person (maybe more).But... You have a simple task, to remove all carriage returns and line feeds that are together indicating blank line(s).So these are the steps you are taking.1. Read file2. Strip Carriage Returns3. Split at Line Feed4. Start at line 15. Each line, check for a value6. Open File/Write to File/Close File7. Continue LoopNow normally, if you were looking for specific strings on a specific line, I'd agree with all but step 6 there, I'd store the information in a variable with CRLF and have them concatenated on the end of each loop where necessary. Then I'd Open the file one time for a write. The speed you'll find (for larger files, or smaller, doesn't really matter) would be much greater then opening the file every loop for a write.However, the OP simply asked how to remove blank lines... There were two proper solutions given IMHO.1. StringReplace - Remove all CRLFCRLF <- indicating blank line(s)2. StringRegExpReplace - Remove all CRLFCRLF (/r = carriage return, /n equals line feed) indicating blank lines, with an option (/s* indicating "possible" spaces only), so that if the line only contains spaces and no other chars, you have the option to remove that line as well.Both options would be basically.1. Read File2. Replace Chars3. Write to fileCutting out looping is going to save many times the speed of the other suggestions (if speed were an issue), and address the issue that the OP had specifically.... Done with my dissertation Edit:Had to fix an ebonic moment, I'm sure there are others. Edited November 1, 2007 by SmOke_N Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer. Link to comment Share on other sites More sharing options...
GEOSoft Posted November 1, 2007 Share Posted November 1, 2007 One of the examples given used FilReadLine which is the slowest of all methods to work with. That one should be avoided whenever pssible. George Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.*** The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number. Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else. "Old age and treachery will always overcome youth and skill!" 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