Jump to content

Delete line in a file


Recommended Posts

Hi guys,

I've been using AutoIT 3 for a week. I want to write a code which reads from a file, matches a string value and if found, deletes the whole line. I know how to open a file and identify the line and the line number where the string matched.

But the problem lies in how do I replace that entire line with "", or in other words, delete the whole line from that file?

Please let me know if any of you know a method to accomplish this task. It's quite urgent.

Thanks & Regards,

Vishwanath Y.R.

Link to comment
Share on other sites

Hi guys,

I've been using AutoIT 3 for a week. I want to write a code which reads from a file, matches a string value and if found, deletes the whole line. I know how to open a file and identify the line and the line number where the string matched.

But the problem lies in how do I replace that entire line with "", or in other words, delete the whole line from that file?

Please let me know if any of you know a method to accomplish this task. It's quite urgent.

Thanks & Regards,

Vishwanath Y.R.

Hi Vishwanath,

This can be achieved as follows:

- open your source file for reading

- create and open a temporary destination file for writing

loop until end of source file

- read 1 line from your source file into memory

- check this line if it matches your string value

- if yes: do nothing

- if not: write that line to the destination file and check for errors

- read next line

close source file and destination file

if you wish you can copy the destination file over the source file.

Hope this helps.

Kind regards,

Arno Rog

Link to comment
Share on other sites

Read the file line by line and ContinueLoop on the line with the string matched.

example:

$file = FileOpen("test.txt", 0)
$outfile = FileOpen("test.txt", 1)
; Check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

; Read in lines of text until the EOF is reached
While 1
    $line = FileReadLine($file)
    If @error = -1 Then ExitLoop
    If StringInStr($line, 'string') Then ContinueLoop; Skip line if "string" found
    FileWriteLine($outfile, $line)
Wend

FileClose($file)
FileClose($outfile)
Link to comment
Share on other sites

Hi Vishwanath,

This can be achieved as follows:

- open your source file for reading

- create and open a temporary destination file for writing

loop until end of source file

- read 1 line from your source file into memory

- check this line if it matches your string value

- if yes: do nothing

- if not: write that line to the destination file and check for errors

- read next line

close source file and destination file

if you wish you can copy the destination file over the source file.

Hope this helps.

Kind regards,

Arno Rog

Link to comment
Share on other sites

Hey Guys,

I tried doing these stuff. The problem is I'm dealing with text files of size a little more than 100 MB. So, copying stuff to a new file is eating up everything of my machine. It would work hell faster if I could just delete the line in the original file itself!!

There's a function called readline which can read a particular line from a file given the line number. I very badly wish to have a writeline () which could write into a file at a particular line.

Hope you got my point!

Thanks & Regards,

Vishwanath Y.R.

Link to comment
Share on other sites

#include <file.au3>
Dim $aRecords
If Not _FileReadToArray("error.log",$aRecords) Then
   MsgBox(4096,"Error", " Error reading log to Array     error:" & @error)
   Exit
EndIf
For $x = 1 to $aRecords[0]
    if stringinstr($aRecords[$x], "yoursearchstring") then
;do whatever you like
Next

just ask the helpfile

Link to comment
Share on other sites

Hi,

i suspect the Array solution may not work with 100Mb file!

Both the above scripts give an error and cannot open the large files;

Try DOS when in doubt ! To Delete all file lines containing "EB100"; 80Mb file in 40secs

;to Delete all file lines containing "EB100"

; 80Mb file in 40secs

_DeleteFileLine(@ScriptDir&"\Err_NC_large80Mb.txt","EB100")

Exit

func _DeleteFileLine($s_file,$s_Exclude)

; Set the file names including temporary

$s_new_file = @ScriptDir&"\newtest.txt"

$s_old_file = @ScriptDir&"\oldfile.txt"

$s_Exclude = '"' & $s_Exclude & '"'

If StringInStr($s_file, " ") Then $s_file = '"' & $s_file & '"'

If StringInStr($s_old_file, " ") Then $s_old_file = '"' & $s_old_file & '"'

If StringInStr($s_new_file, " ") Then $s_new_file = '"' & $s_new_file & '"'

; Set the Command and Run Dos

$s_Command = 'type '&$s_file&' | find /v '&$s_Exclude&' > '&$s_new_file

RunWait(@ComSpec & " /c " & $s_Command)

; Rename to OldFile

$s_Command = 'rename '&$s_file&' '&$s_old_file

RunWait(@ComSpec & " /c " & $s_Command)

$s_Command = 'rename '&$s_new_file&' '&$s_file

RunWait(@ComSpec & " /c " & $s_Command)

endfunc ;===>_DeleteFileLine

Otherwise, you may need to find a direct disc editing program and automate that?

Best, Randall

Best, Randall

Edited by randallc
Link to comment
Share on other sites

Hi Randal,

Your program works cool, but I still have a problem!!! The find command takes just 1 string as an argument to search. But I currently have a set of 50 strings, the number will definitely increase!!, and I want it to parse all the lines to check for those 50 strings and then delete the line which contains any of those strings. If I run a loop, clearly it takes 45 minutes for a 500K lines file. That still is not feasible.

There exists a chomp() in Perl scripts and a few others, aint there anything like that which would help me here?

Thanks & Regards,

Vishwanath Y.R.

Link to comment
Share on other sites

hi,

I have fixed the script, I think, to use "Findstr" from Dos instead;

multiple data in a string for exclusions, separated by spaces (or use the /G:FileName option)

;to Delete all file lines containing any of Data1, Data2 ...etc [separated in the string by spaces; or..

; Else look into "findstr" in Dos and retrieve the strings to avoid from a file instead (/G:[Filename])

; 80Mb file in 40secs

;_DeleteFileLine(@ScriptDir&"\Err_NC_large80Mb.txt","EB100")

$s_Exclude="Data1 Data2 Data3 Data4 Data5 Data6 Data7 Data8 Data9 Data10 Data11"

_DeleteFileLine(@ScriptDir&"\Table1.txt",$s_Exclude)

Exit

func _DeleteFileLine($s_file,$s_Exclude)

; Set the file names including temporary

FileChangeDir(@ScriptDir)

$OrigFile=StringReplace($s_file,@ScriptDir&"\","")

$s_old_file = "oldfile.txt"

$s_new_file = @ScriptDir&"\newtest.txt"

$s_Exclude = '"' & $s_Exclude & '"'

If StringInStr($s_file, " ") Then $s_file = '"' & $s_file & '"'

If StringInStr($s_old_file, " ") Then $s_old_file = '"' & $s_old_file & '"'

If StringInStr($s_new_file, " ") Then $s_new_file = '"' & $s_new_file & '"'

If StringInStr($OrigFile, " ") Then $OrigFile = '"' & $OrigFile & '"'

; Set the Command and Run Dos

$s_Command = 'type | findstr /v '&$s_Exclude&' '&$s_file&' > '&$s_new_file

RunWait(@ComSpec & " /c " & $s_Command,@ScriptDir,@SW_HIDE)

; Rename Original to OldFile

if FileExists(@ScriptDir&"\"&$s_old_file) then FileDelete(@ScriptDir&"\"&$s_old_file)

$s_Command = 'rename '&$s_file&' '&$s_old_file

RunWait(@ComSpec & " /c " & $s_Command,@ScriptDir,@SW_HIDE)

; Rename Newfile to Original

if FileExists(@ScriptDir&"\"&$OrigFile) then FileDelete(@ScriptDir&"\"&$OrigFile)

$s_Command = 'rename '&$s_new_file&' '&$OrigFile

RunWait(@ComSpec & " /c " & $s_Command,@ScriptDir,@SW_HIDE)

endfunc ;===>_DeleteFileLine

Best, Randall
Link to comment
Share on other sites

Hi guys,

I've been using AutoIT 3 for a week. I want to write a code which reads from a file, matches a string value and if found, deletes the whole line. I know how to open a file and identify the line and the line number where the string matched.

But the problem lies in how do I replace that entire line with "", or in other words, delete the whole line from that file?

Please let me know if any of you know a method to accomplish this task. It's quite urgent.

Thanks & Regards,

Vishwanath Y.R.

This is a bit taken from something I wrote which read a file and replaced a line of text if it's any use to you.

$file = FileOpen($Path & "Messages\English\messages.txt", 0)
_FileCreate ( $Path & "Messages\English\Messages.tmp" )
$NewFile = FileOpen ( $Path & "Messages\English\Messages.tmp", 2 )

; Check if file opened for reading OK
    If $file = -1 Then
        MsgBox(0, "Error", "Unable to open Messages.txt")
        Exit
    EndIf

; Read in lines of text until the EOF is reached
    While 1
        $line = FileReadLine($file)
        If @error = -1 Then ExitLoop

        $result = StringInStr($line, "IDS_PAPER_TYPE_MATTE  ""Matte""")
        If $Result =0 then
    ;do nothing but wait until Filewriteline to copy original text  to tmp file 
        Else 
        $Line = @tab & "IDS_PAPER_TYPE_MATTE    ""Lustre"""
        Endif
        FileWriteLine ($NewFile, $Line)
    Wend
    
    FileClose ( $File )
    FileClose ( $NewFile )
    FileDelete ( $Path & "Messages\English\messages.txt" )
    FileMove($Path & "Messages\English\Messages.tmp", $Path & "Messages\English\Messages.txt")
Link to comment
Share on other sites

  • 4 years later...

This to delete Line that containing a "This String" on file Test.txt

I Make it as simple as I can.

#include <file.au3>
Dim $aRecords
_FileReadToArray("Test.txt",$aRecords)
For $x = 1 to $aRecords[0]
    if stringinstr($aRecords[$x], "This String") then _FileWriteToLine("Test.txt", $x, "", 1) 
;do whatever you like
Next

And

if stringinstr($aRecords[$x], "This String") OR stringinstr($aRecords[$x], "This String")

if you like to delete more than one string

Edited by Z4K1
Link to comment
Share on other sites

  • Moderators

Z4K1,

Welcome to the AutoIt forum. :blink:

Did you notice the date on the post immediately preceding yours? After 5 years I am not sure that the topic is still in need of a solution! ;)

Please try and pick a more current thread for your next post. :P

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

  • 3 weeks later...

thks z4k1 (:blink:) I happen to need something very similar. I have been cracking my head on how to rename pdf files based on it content by matching some companies name which will appear in those pdf files. Project still on going ;)

Link to comment
Share on other sites

  • 1 year later...

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