Vishwanath Y.R. Posted October 24, 2005 Share Posted October 24, 2005 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 More sharing options...
awrog Posted October 24, 2005 Share Posted October 24, 2005 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 More sharing options...
MHz Posted October 24, 2005 Share Posted October 24, 2005 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 More sharing options...
Vishwanath Y.R. Posted October 24, 2005 Author Share Posted October 24, 2005 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 More sharing options...
Vishwanath Y.R. Posted October 24, 2005 Author Share Posted October 24, 2005 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 More sharing options...
Nuffilein805 Posted October 24, 2005 Share Posted October 24, 2005 #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 my little chatmy little encryption toolmy little hidermy unsafe clickbot Link to comment Share on other sites More sharing options...
randallc Posted October 24, 2005 Share Posted October 24, 2005 (edited) 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")Exitfunc _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_fileRunWait(@ComSpec & " /c " & $s_Command); Rename to OldFile$s_Command = 'rename '&$s_file&' '&$s_old_fileRunWait(@ComSpec & " /c " & $s_Command)$s_Command = 'rename '&$s_new_file&' '&$s_fileRunWait(@ComSpec & " /c " & $s_Command)endfunc ;===>_DeleteFileLineOtherwise, you may need to find a direct disc editing program and automate that?Best, RandallBest, Randall Edited October 24, 2005 by randallc ExcelCOM... AccessCom.. Word2... FileListToArrayNew...SearchMiner... Regexps...SQL...Explorer...Array2D.. _GUIListView...array problem...APITailRW Link to comment Share on other sites More sharing options...
Vishwanath Y.R. Posted October 25, 2005 Author Share Posted October 25, 2005 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 More sharing options...
randallc Posted October 25, 2005 Share Posted October 25, 2005 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)Exitfunc _DeleteFileLine($s_file,$s_Exclude); Set the file names including temporaryFileChangeDir(@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_fileRunWait(@ComSpec & " /c " & $s_Command,@ScriptDir,@SW_HIDE); Rename Original to OldFileif FileExists(@ScriptDir&"\"&$s_old_file) then FileDelete(@ScriptDir&"\"&$s_old_file)$s_Command = 'rename '&$s_file&' '&$s_old_fileRunWait(@ComSpec & " /c " & $s_Command,@ScriptDir,@SW_HIDE); Rename Newfile to Originalif FileExists(@ScriptDir&"\"&$OrigFile) then FileDelete(@ScriptDir&"\"&$OrigFile)$s_Command = 'rename '&$s_new_file&' '&$OrigFileRunWait(@ComSpec & " /c " & $s_Command,@ScriptDir,@SW_HIDE)endfunc ;===>_DeleteFileLineBest, Randall ExcelCOM... AccessCom.. Word2... FileListToArrayNew...SearchMiner... Regexps...SQL...Explorer...Array2D.. _GUIListView...array problem...APITailRW Link to comment Share on other sites More sharing options...
ChrisL Posted October 25, 2005 Share Posted October 25, 2005 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") [u]Scripts[/u]Minimize gui to systray _ Fail safe source recoveryMsgbox UDF _ _procwatch() Stop your app from being closedLicensed/Trial software system _ Buffering Hotkeys_SQL.au3 ADODB.Connection _ Search 2d Arrays_SplashTextWithGraphicOn() _ Adjust Screen GammaTransparent Controls _ Eventlogs without the crap_GuiCtrlCreateFlash() _ Simple Interscript communication[u]Websites[/u]Curious Campers VW Hightops Lambert Plant Hire Link to comment Share on other sites More sharing options...
Z4K1 Posted July 23, 2010 Share Posted July 23, 2010 (edited) This to delete Line that containing a "This String" on file Test.txtI 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 NextAndif stringinstr($aRecords[$x], "This String") OR stringinstr($aRecords[$x], "This String")if you like to delete more than one string Edited July 23, 2010 by Z4K1 Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted July 23, 2010 Moderators Share Posted July 23, 2010 Z4K1, Welcome to the AutoIt forum. 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. M23 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
Zom Posted August 10, 2010 Share Posted August 10, 2010 thks z4k1 () 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 More sharing options...
Guest Posted August 24, 2011 Share Posted August 24, 2011 (edited) Oops sorry... Edited August 24, 2011 by Guest 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