iiyama Posted April 25, 2010 Share Posted April 25, 2010 Hi, I was hoping someone could give me some advice on a search problem I have. I'm wanting to search an Array for a word more than once and capture either the line number of the found words "that I could then copy the lines into a text file" or the sentence of the line/s. I can only seem to capture a word once and then it stops the search. My aim is to search for a word then copy the lines containg that word into a new txt file. Below is what I have come up with. It maybe a line I'm missing to make it continue searching. So I hope someone could help or suggest a better way of doing it? Example text/array that would be searched... I have a new house <<<<<< would be copied I have a old house I have a new car <<<<<< these two lines would be copied to the msg box in my example below. Aim is for a text file. but my problem is the continued search. Example script. #include <Array.au3> #include <file.au3> Dim $aRecords _FileReadToArray("c:\new.INI",$aRecords) $sSearch = "new" ;<== search text $iIndex = _ArraySearch($aRecords, $sSearch, 0, 0, 0, 1) If $iIndex = -1 Then Else MsgBox(262144+48, "Found", $aRecords[$iIndex]) EndIf Any help would be appreciated. Thanks. Link to comment Share on other sites More sharing options...
JohnOne Posted April 25, 2010 Share Posted April 25, 2010 1. you could put what you are doing into a loop 2. you could use the _ArrayFindAll() function 3. you could use the iniread/inireadsection functions, if as indicated the file is in standard ini format Just read up on the functions mentioned, and have another pop. AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
jchd Posted April 25, 2010 Share Posted April 25, 2010 iiyama, Do you want lines where the substring "new" appears or the lines where the word "new" appears? That's not the same: I knew I could do it. <<< would that qualify? I've renewed my domain. <<< would that qualify? This item is like new. <<< would that qualify? (due to final dot) I believe you can do all with a regexp to select an array of complete matching lines, but you have to be quite precise in what you want exactly. Also will there be special characters in the substring (or word) you will be searching? 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 hereRegExp tutorial: enough to get startedPCRE 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 More sharing options...
iiyama Posted April 25, 2010 Author Share Posted April 25, 2010 Thanks for the feed back but I still can't get it to do what I need. The file isn't an INI file so iniread/inireadsection functions wouldn't work I assume. I labeled it an INI so that the icons are different compaired to other formats on my computer. Sorry for the confusion. The files I would be using/searching are plain text files. I had a look at _ArrayFindAll() as suggested but I couldn't get it to work for some reason and I'm not sure how to make a loop operate as when I tried the loop option before I posted my orignal question I would simply have an infanite loop of the first search and not procced to the next or even end. Maybe you could post some examples for me for the _ArrayFindAll() or how I could add a loop to my orignal code? Thanks again for any help. Link to comment Share on other sites More sharing options...
jchd Posted April 25, 2010 Share Posted April 25, 2010 Just answer the questions I asked! 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 hereRegExp tutorial: enough to get startedPCRE 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 More sharing options...
iiyama Posted April 25, 2010 Author Share Posted April 25, 2010 (edited) Just answer the questions I asked!I was orignaly replying to the first reply from JohnONE.I would need the Subsring "new" so any of the examples you posted would be required to be found.I knew I could do it. <<< would that qualify?I've renewed my domain. <<< would that qualify? This item is like new. <<< would that qualify? (due to final dot) Edited April 25, 2010 by iiyama Link to comment Share on other sites More sharing options...
iiyama Posted April 25, 2010 Author Share Posted April 25, 2010 forgot to say that Special Characters could appear in the search string. Link to comment Share on other sites More sharing options...
JohnOne Posted April 25, 2010 Share Posted April 25, 2010 heres a quick and dirty example #include <Array.au3> #include <File.au3> Dim $aFirst, $aSecond[1] $FilePath = @ScriptDir & "\test.txt" _FileReadToArray($FilePath, $aFirst) _ArrayDisplay($aFirst) For $i = 1 To $aFirst[0] If StringInStr($aFirst[$i], "new") Then _ArrayAdd($aSecond, $i) EndIf Next _ArrayDisplay($aSecond) For $i = 0 To UBound($aSecond) -1 ConsoleWrite("Line number " & $aSecond[$i] & " : " & $aFirst[$aSecond[$i]] & @CRLF) Next remember this is just an example and there will definately more fancy ways to do this, just alter the $FilePath to test it. AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
jchd Posted April 25, 2010 Share Posted April 25, 2010 Sorry for delay (Sunday familly lunch...) Then try this: #include <File.au3> #include <Array.au3> Local $test[8] = [ _ "I knew I could do it.", _ " (-'""è_) more stuff without the ne_w thing", _ "I've renewed my domain.", _ "This item is like new.", _ "This line won't get copied either", _ "I have a new house", _ "I have a old house", _ "I have a new car" _ ] Global $file = "text.txt" _FileWriteFromArray($file, $test) ; --------------------------- Local $str = FileRead($file) Local $search = 'new' Local $a = StringRegExp($str, '(?:\n|\A)(.*\Q' & $search & '\E.*)(?:\r|\z)', 3) _ArrayDisplay($a) FileDelete($file) 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 hereRegExp tutorial: enough to get startedPCRE 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 More sharing options...
iiyama Posted April 25, 2010 Author Share Posted April 25, 2010 heres a quick and dirty example #include <Array.au3> #include <File.au3> Dim $aFirst, $aSecond[1] $FilePath = @ScriptDir & "\test.txt" _FileReadToArray($FilePath, $aFirst) _ArrayDisplay($aFirst) For $i = 1 To $aFirst[0] If StringInStr($aFirst[$i], "new") Then _ArrayAdd($aSecond, $i) EndIf Next _ArrayDisplay($aSecond) For $i = 0 To UBound($aSecond) -1 ConsoleWrite("Line number " & $aSecond[$i] & " : " & $aFirst[$aSecond[$i]] & @CRLF) Next remember this is just an example and there will definately more fancy ways to do this, just alter the $FilePath to test it. Thanks for the example it seems to do the job of showing all the correct search values and lines assoicated with them. The only problem now is on how to get the searchs and text on that row into a variable that I could then insert into a blank notepad/text file or msgbox. I think I understand what consolewrite is and that it writes the values inside SciTE. But I need to capture the consolewrite findings. I was thinking it was just the case of using the values in the ConsoleWrite line and inserting them into a msgbox, new text file using a FileWrite function but I can't get it to work. like $Finalresults = ConsoleWrite("Line number " & $aSecond[$i] & " : " & $aFirst[$aSecond[$i]] & @CRLF) MsgBox(262144+48, "Found", $Finalresults) Thanks again for any help and sorry to be a pain. Link to comment Share on other sites More sharing options...
jchd Posted April 25, 2010 Share Posted April 25, 2010 No problem. Using the test.txt file you can recreate in the first part of my previous example: #include <File.au3> #include <Array.au3> ;--------------------- create a test file Local $test[8] = [ _ "I knew I could do it.", _ " (-'""è_) more stuff without the ne_w thing", _ "I've renewed my domain.", _ "This item is like new.", _ "This line won't get copied either", _ "I have a new house", _ "I have a old house", _ "I have a new car" _ ] Global $file = "text.txt" _FileWriteFromArray($file, $test) ; ----------------------- this is the search part Local $str = FileRead($file) Local $search = 'new' Local $a = StringRegExp($str, '(?:\n|\A)(.*\Q' & $search & '\E.*)(?:\r|\z)', 3) Local $str = _ArrayToString($a, @CRLF) MsgBox(0, "Found lines", $str) ; You can write the resulting multiline string to where you want: Msgox as above, file, NotePad, the Moon, ... anywhere FileDelete($file) 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 hereRegExp tutorial: enough to get startedPCRE 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 More sharing options...
JohnOne Posted April 25, 2010 Share Posted April 25, 2010 (edited) For the record, I think jchds method is much cleaner, but for the purpose of this example. #include <Array.au3> #include <File.au3> Dim $aFirst, $aSecond[1] $FilePath = @ScriptDir & "\test.txt" _FileReadToArray($FilePath, $aFirst) _ArrayDisplay($aFirst) For $i = 1 To $aFirst[0] If StringInStr($aFirst[$i], "new") Then _ArrayAdd($aSecond, $i) EndIf Next _ArrayDisplay($aSecond) For $i = 1 To UBound($aSecond) -1 $sFinalString &= "Line number " & $aSecond[$i] & " : " & $aFirst[$aSecond[$i]] & @CRLF Next MsgBox(0,"Result",$sFinalString) EDIT: To make the search case sensitive, cange the reletive line to this If StringInStr($aFirst[$i], "new",1) Then Edited April 25, 2010 by JohnOne AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
iiyama Posted April 25, 2010 Author Share Posted April 25, 2010 No problem. Using the test.txt file you can recreate in the first part of my previous example: #include <File.au3> #include <Array.au3> ;--------------------- create a test file Local $test[8] = [ _ "I knew I could do it.", _ " (-'""è_) more stuff without the ne_w thing", _ "I've renewed my domain.", _ "This item is like new.", _ "This line won't get copied either", _ "I have a new house", _ "I have a old house", _ "I have a new car" _ ] Global $file = "text.txt" _FileWriteFromArray($file, $test) ; ----------------------- this is the search part Local $str = FileRead($file) Local $search = 'new' Local $a = StringRegExp($str, '(?:\n|\A)(.*\Q' & $search & '\E.*)(?:\r|\z)', 3) Local $str = _ArrayToString($a, @CRLF) MsgBox(0, "Found lines", $str) ; You can write the resulting multiline string to where you want: Msgox as above, file, NotePad, the Moon, ... anywhere FileDelete($file) Hi jchd, Thanks for the reply. I think I'm almost sorted using your routine. I just have one more question. Is it possible to make it search in lower or uppercase so it could find say Test test tEst I expect I could force the files text to smallcase but it would be much nicer if the search sript didn't mind and I could then use both. Thanks again. this is what I'm using from your example #include <File.au3> #include <Array.au3> $file = "c:\temp\text.INI" ; ----------------------- this is the search part $str = FileRead($file) $search = 'tEst' $a = StringRegExp($str, '(?:\n|\A)(.*\Q' & $search & '\E.*)(?:\r|\z)', 3) $str = _ArrayToString($a, @CRLF) MsgBox(0, "Found lines", $str) ; You can write the resulting multiline string to where you want: Msgox as above, file, NotePad, the Moon, ... anywhere Link to comment Share on other sites More sharing options...
iiyama Posted April 25, 2010 Author Share Posted April 25, 2010 Hi again, I was able to change it to include the case-insensitivity flag. Thanks to both of you for all your help. Link to comment Share on other sites More sharing options...
jchd Posted April 25, 2010 Share Posted April 25, 2010 Good, you found the (?i) flag. For this "problem" like some others, I can't pretend regexps are the key to IT heaven, but they can reveal extremely powerful and consistent (when not abused). 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 hereRegExp tutorial: enough to get startedPCRE 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 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