Jump to content

Remove multiple lines from txt file


Recommended Posts

Hi. I have a text file with (for example) 10 lines of text

How to remove lets say a range of 5-9 using line numbers as variables ?

Func _RemoveLines()
    ;Starting and ending line numbers are $StartLineToRemove and $EndLineToRemove
    For $S =  $StartLineToRemove to $EndLineToRemove
        ;Delete $S line
    Next
    MsgBox(0,'','DONE')
EndFunc

File can be very large, it can be 20MB size and range can start anywhere from the begining to almost the end of the file, so quick way is a must.

I tried using array thing but its hard for me to figure it out. Can someone help ?

Thanks !

Link to comment
Share on other sites

The easiest way to do it using the UDFs that come with AutoIt is something like this.

#include <File.au3>
$aArray = FileReadToArray("Filename")
$hFile = FileOpen("NewFileName", 2) ; open a file, deleting the contents if it already exists
_FileWriteFromArray($hFile, $aArray, 1, 4) ; write lines 1 - 4 to file
_FileWriteFromArray($hFile, $aArray, 10) ; write lines 10 - EOF to file
FileClose($hFile)

You can always use variables for the 1, 4 and 10 in the FileWrite* lines.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Maybe Try something like this if they are always in a group...or make multiple calls if there are multiple groups

;;Includes
#include <File.au3>
#include <MsgBoxConstants.au3>



;;Variables
$LineStart =3
$LineEnd =5
$LineStart2 =7
$LineEnd2 =10
;;Type the path of the file you'd like to modify
$sFilePath = "C:\Temp" & "\Test.txt"


;;Your Function Call
_DeleteLines($LineStart, $LineEnd)
_DeleteLines($LineStart2, $LineEnd2)

;;Your Function
Func _DeleteLines ($LS, $LE)
    If $LS < $LE Then ;; Can't go backwards...
        For $i = $LS To $LE
            $line = _FileWriteToLine($sFilePath, $i, "", 1)
        Next
    EndIf
EndFunc

;;Might need to add some sleep timers if its not working...

I suck at programming... But I try really hard! :imwithstupid:

Link to comment
Share on other sites

 

Maybe Try something like this if they are always in a group...or make multiple calls if there are multiple groups

;;Includes
#include <File.au3>
#include <MsgBoxConstants.au3>



;;Variables
$LineStart =3
$LineEnd =5
$LineStart2 =7
$LineEnd2 =10
;;Type the path of the file you'd like to modify
$sFilePath = "C:\Temp" & "\Test.txt"


;;Your Function Call
_DeleteLines($LineStart, $LineEnd)
_DeleteLines($LineStart2, $LineEnd2)

;;Your Function
Func _DeleteLines ($LS, $LE)
    If $LS < $LE Then ;; Can't go backwards...
        For $i = $LS To $LE
            $line = _FileWriteToLine($sFilePath, $i, "", 1)
        Next
    EndIf
EndFunc

;;Might need to add some sleep timers if its not working...

Thanks for this example. It made since but it worked rather weird. It removes different sections (range of lines containing layers information) and it removes then in no particular order.

Take a look at my code and see.

Here is how it works.

Start the script ( AutoIt Version: 3.3.12.0)

G-Code Modifier.au3

Click Open G-Code  downloaded/zip extracted file. Let it finish searching.

10x10x10 CUBE.zip

Click Layer 1 (wait a second) and click "<<Selected" on the bottom left (wait a second)

Click Layer 20 (wait a second) and click "Selected>>" on the bottom right (wait a second)

Click Remove (Wait until mesage box says "DONE")

Button triggered in main while 1 loop:

If $msg = $RemoveSelected Then

Button triggers your function in at the very bottom:

Func _RemoveLayers ($LS, $LE)

 

Not only it takes a while to remove these lines in such a small file, but it also does it completely random :(

Please someone have a look, this program is a contribution to 3d printing communities.

Thanks !

Link to comment
Share on other sites

#include <File.au3>

Local $file = "C:\Intel\Test2.txt"
Local $lines[2] = [3 , 7]
Local $array, $erase, $content, $fh

_FileReadToArray($file, $array)
$erase = ""
For $i = $lines[0] To $lines[1]
   $erase = $erase & $array[$i] & @CRLF
   MsgBox(0,'',$erase)
Next
$content = FileRead($file)
$content = StringRegExpReplace($content,"\h*\Q" & $erase & "\E.*\cM\cJ","")
$fh = FileOpen($file,2)
FileWrite($fh, $content)
FileClose($fh) 

Here's the code I've been using to replace lines. Although this is based off of content and not line number, I modified it a little so that you can input line numbers. The part I use is the last 5 lines. I can't guarantee this will be fast but at least it'll work.

Edited by Aphotic
Link to comment
Share on other sites

#include <File.au3>

Local $file = "C:\Intel\Test2.txt"
Local $lines[2] = [3 , 7]
Local $array, $erase, $content, $fh

_FileReadToArray($file, $array)
$erase = ""
For $i = $lines[0] To $lines[1]
   $erase = $erase & $array[$i] & @CRLF
   MsgBox(0,'',$erase)
Next
$content = FileRead($file)
$content = StringRegExpReplace($content,"\h*\Q" & $erase & "\E.*\cM\cJ","")
$fh = FileOpen($file,2)
FileWrite($fh, $content)
FileClose($fh) 

Here's the code I've been using to replace lines. Although this is based off of content and not line number, I modified it a little so that you can input line numbers. The part I use is the last 5 lines. I can't guarantee this will be fast but at least it'll work.

Where do i specify lines ?

Do i specify starting and ending line or each line or what ?

Am not sure how the code works.

Please clearify.

Thanks !

Link to comment
Share on other sites

Since i didnt understand the above example i came up with this:

$Text = ""
_FileReadToArray ($Source,$Text)
$NewText = _ArrayDelete ($Text, $StartLineToRemove-1 & "," &  $EndLineToRemove)
_FileWriteFromArray (@DesktopDir & "\New Code.gcode",$NewText)
MsgBox(0,'',"Done")

But it deletes entire array leaving only 27 lines of text deleting remaining 7000+ lines.

EDIT:

I figured that _arraydelete does not return array with removed rows, so is there  a way to delete rows and return new array so i can write it to a file ?

Edited by tonycst
Link to comment
Share on other sites

This works

Func _RemoveLayers ($LS, $LE)

Local $array = FileReadToArray($Source)   ; ("10x10x10 CUBE.gcode")
_ArrayDelete($array, $LS & "-" & $LE-2)   ; delete range
_ArrayDisplay($array)
; $txt = _ArraytoString($array, @crlf)
; FileWrite("result.txt", $txt)

EndFunc

But be careful with indexes, asking to remove layers 4 to 8  this removes layers 3 (included) to 6 (included) in the file (check _ArrayDisplay)

Edit

So does Aphotic's code :)

Edited by mikell
Link to comment
Share on other sites

Where do i specify lines ?

Do i specify starting and ending line or each line or what ?

Am not sure how the code works.

Please clearify.

Thanks !

3 is the starting line and 7 is the ending line. Sorry, thought you'd pick up on that.

Local $lines[2] = [3 , 7]

Link to comment
Share on other sites

:) yeah but no :(

Anyway i figured another way of doing it. I finaly reagured how _arraydelete works to delete the range.

I h ave no clue (fail to pay attention to the manual) that it modifies the array rather then returning new array.

So all i did was:

$Array = ""
_FileReadToAray ("file.txt",$Array)
Local $Range = ($StartLineToRemove & '-' & $EndLineToRemove-1)
_ArrayDelete ($ArrayGlobal,$Range)
_FileWriteFromArray (FileSaveDialog ("Save file as" ,"","All (*.*)",$FD_PROMPTOVERWRITE, "NewGcode_Removed_Layers.Gcode",$MainGUI),$ArrayGlobal)

Stupid me :)

 

THanks for all your help. I've learned allot about arrays to get me going with improvements :)

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