Sign in to follow this  
Followers 0
COD3369

Delete set of lines from file ?

16 posts in this topic

i want to delete some lines from a text file.

i want to delete line number 1 to 57

any idea how?

Share this post


Link to post
Share on other sites



Hi,

This question was asked a dozen of times so yes you can.

Br, FireFox.


 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


Link to post
Share on other sites

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


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

_FileWriteToLine() is the function you need.

Too late. M23 got there before me.

Edited by czardas

Share this post


Link to post
Share on other sites

Melba23 any example ?

1. autoit is cool
2. autoit is cool
3 autoit is cool
4 autoit is cool
5 autoit is cool
6 autoit is cool
7 autoit is cool
8 autoit is cool
9 autoit is cool

delete line number 4 to 7

Share this post


Link to post
Share on other sites

verma123,

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

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

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


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites

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


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
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)

Share this post


Link to post
Share on other sites

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

1 person likes this

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites

I was away.

1 person likes this

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)

Share this post


Link to post
Share on other sites

Guys you rocked !!

Melba23 Malkey Danyfirex

 

its working flawlessly :D thank you very much guys love you all :)

Share this post


Link to post
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 ?

Share this post


Link to post
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
Sign in to follow this  
Followers 0