Jump to content

Delete set of lines from file ?


Recommended Posts

  • Moderators

verma123,

You could read the file into an array with _FileReadToArray and then write it back using FileWriteFromArray but setting the $iBase parameter to 58. :)

Or use_FileWriteToLine in a loop to delete the required lines - in this case you would either need to loop from line #57 up or continually delete just the first line. ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • Moderators

verma123,

And what have you tried that has not worked? I gave you enough hints. ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Hi. maybe something like this.

#include <File.au3>
#include <Array.au3>

Local $aRecords
If Not _FileReadToArray("1.txt", $aRecords) Then
    MsgBox(4096, "Error", " Error reading log to Array     error:" & @error)
    Exit
EndIf
For $x = 1 To $aRecords[0]
    MsgBox(0, 'Record:' & $x, $aRecords[$x])
Next


_ArrayDelete($aRecords,4)
_ArrayDelete($aRecords,7-1)

_FileWriteFromArray("2.txt",$aRecords,1)

saludos

Edited by Danyfirex
Link to comment
Share on other sites

  • Moderators

verma123,

And does that look like what I suggested? Go and read again what I wrote - I gave you 2 possible solutions and you have mixed them up in the same script. ;)

First Solution: Read the file into an array and then rewrite it but not from the same start point.

Second Solution: Loop through the file using _FileWriteToLine and delete the lines in a loop. Have you even looked at the _FileWriteToLine page in the Help file? Does the syntax you have used look as if it will work? Where is the $iLine parameter? Are you sure that the $fOverWrite parameter should be set to 0 if you want to delete lines?

Over to you for a better effort! :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • Moderators

verma123,

It is not that difficult. Save those few lines as "Cool.txt" and then run this:

#include <File.au3>
#include <Array.au3>

Local $aLines
_FileReadToArray("Cool.txt", $aLines)

; Delete first 3 lines
_FileWriteFromArray("Cool-Array-Start.txt", $aLines, 4)

; Delete lines from middle
For $i = 7 to 4 Step -1
    _ArrayDelete($aLines, $i)
Next
_FileWriteFromArray("Cool-Array-Mid.txt", $aLines, 1)

; Delete lines in a loop
FileCopy("Cool.txt", "Cool_WriteToLine.txt", $FC_OVERWRITE)
For $i = 7 to 4 Step -1
    _FileWriteToLine("Cool_WriteToLine.txt", $i, "", 1)
Next
I hope all that is clear enough for you to develop it to fit your requirements. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Here is a regular expression replace method.

Opt("WinTitleMatchMode", 2) ;1=start, 2=subStr, 3=exact, 4=advanced, -1 to -4=Nocase

; ------------- Create File --------------
Local $sFileName = "Test9Lines.txt"
Local $sFileContents
For $i = 1 To 9
    $sFileContents &= $i & ". autoit is cool" & @CRLF
Next
If FileExists($sFileName) Then
    FileDelete($sFileName)
EndIf
FileWrite($sFileName, StringTrimRight($sFileContents, 2))
ShellExecute($sFileName)
WinWaitActive($sFileName)
; ----------> End of Create File ---------

Local $sNewFileName = "TestMinusLines.txt"
Local $iStart = 4
Local $iTo = 7

If $iStart < 1 Then $iStart = 1 ; First line is line #1.
If FileExists($sNewFileName) Then
    FileDelete($sNewFileName)
EndIf
FileWrite($sNewFileName, StringRegExpReplace(FileRead($sFileName), "(?s)((\V*\v*){" & $iStart - 1 & "})(\V+\v*){" & ($iTo - $iStart + 1) & "}(.*$)", "\1\4"))

ShellExecute($sNewFileName)

;Tidy up files
WinWaitActive($sNewFileName)
FileDelete($sFileContents)
FileDelete($sNewFileName)
Link to comment
Share on other sites

  • Moderators

Malkey,

I was having an internal wager whether it would be you or jchd who posted an SRE solution - my left hand won! :D

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

I was away.

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

Hi Malky,

in this line,

FileWrite($sNewFileName, StringRegExpReplace(FileRead($sFileName), "(?s)((\V*\v*){" & $iStart - 1 & "})(\V+\v*){" & ($iTo - $iStart + 1) & "}(.*$)", "\1\4"))

what does {" & $iStart - 1 & "} mean?

and

((\V*\v*){" & $iStart - 1 & "})  ...does the outside parenthesis represent for \2 ?
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...