kris06 Posted July 2, 2008 Share Posted July 2, 2008 Hi all, I have a file which contains data in the following order:file.txt:=====192.168.1.1 || root192.168.1.2 || admin....etcmy search is through a gui, based on the IP address. so basically when a user enters 192.168.1.1. I need to find that string in this file.txt and thenretrieve that line, and then split it and retrieve the "root" string.I am not sure of a method that would help me retrieve the line from a file based on a search string. Any help would be greatly appreciated.Thanks,Kris Link to comment Share on other sites More sharing options...
kris06 Posted July 2, 2008 Author Share Posted July 2, 2008 Hi all, I have a file which contains data in the following order:file.txt:=====192.168.1.1 || root192.168.1.2 || admin....etcmy search is through a gui, based on the IP address. so basically when a user enters 192.168.1.1. I need to find that string in this file.txt and thenretrieve that line, and then split it and retrieve the "root" string.I am not sure of a method that would help me retrieve the line from a file based on a search string. Any help would be greatly appreciated.Thanks,KrisIf there is a way to get the line number based on a partial string search, then i can use the filereadline method to get the entire line. Link to comment Share on other sites More sharing options...
weaponx Posted July 2, 2008 Share Posted July 2, 2008 Pseudocode: _FileReadToArray For $X = 1 to $Array[0] If StringInStr Then EndIf Next Link to comment Share on other sites More sharing options...
Xenobiologist Posted July 2, 2008 Share Posted July 2, 2008 #include <Array.au3> $string = '192.168.1.1 || root' & @CRLF & '192.168.2.2 || admin' & @CRLF & '192.168.3.1 || root' MsgBox(0,0, StringStripCR(_getName($string, '192.168.2.2'))) Func _getName($string, $suche) Local $re = StringRegExp($string, $suche & '\s*\|\|\s*(.*)', 3) Return $re[0] EndFunc ;==>_getName Scripts & functions Organize Includes Let Scite organize the include files Yahtzee The game "Yahtzee" (Kniffel, DiceLion) LoginWrapper Secure scripts by adding a query (authentication) _RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...) Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc. MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times Link to comment Share on other sites More sharing options...
kris06 Posted July 2, 2008 Author Share Posted July 2, 2008 #include <Array.au3> $string = '192.168.1.1 || root' & @CRLF & '192.168.2.2 || admin' & @CRLF & '192.168.3.1 || root' MsgBox(0,0, StringStripCR(_getName($string, '192.168.2.2'))) Func _getName($string, $suche) Local $re = StringRegExp($string, $suche & '\s*\|\|\s*(.*)', 3) Return $re[0] EndFunc ;==>_getName its going to be a file with endless IP addresses || login names, I cannot hardcode it. Is there no way to find a particular string in a file and return the line number(first occurence)where it was found? Link to comment Share on other sites More sharing options...
weaponx Posted July 2, 2008 Share Posted July 2, 2008 its going to be a file with endless IP addresses || login names, I cannot hardcode it.Is there no way to find a particular string in a file and return the line number(first occurence)where it was found?We need some effort on your part...that was just a demonstration.$string - FileRead("somefile.txt") Link to comment Share on other sites More sharing options...
kris06 Posted July 2, 2008 Author Share Posted July 2, 2008 We need some effort on your part...that was just a demonstration.$string - FileRead("somefile.txt")heres an example snippet of wht i have.$find = "192.168.1.1" ;assuming GUI returned this value$string = FileRead("somefile.txt") ;String now has the entire file contents$ret = StringInStr($string, $find) ; this only will give the position of the substring where it was found. what i need is the linenumber ..... Link to comment Share on other sites More sharing options...
weaponx Posted July 2, 2008 Share Posted July 2, 2008 #include <file.au3> Dim $array Dim $search = "192.168.1.1" _FileReadToArray("myfile.txt", $array) For $X = 1 to $Array[0] If StringInStr($array[$X], $search) Then MsgBox(0,"Match found", "Line number: " & $X) EndIf Next Link to comment Share on other sites More sharing options...
kris06 Posted July 2, 2008 Author Share Posted July 2, 2008 In my hurry, i never saw xenobiologist's reply, heres wht I have, i get a "subscript used with non-array variable" exception heres wht i have : ============ $remip = guictrlread($ip1) $passfile = Fileopen("somtext.txt", 0) $string = fileread($passfile) MsgBox(0,0, StringStripCR(_getName($string, $remip))) Func _getName($string, $suche) Local $re = StringRegExp($string, $suche & '\s*\|\|\s*(.*)', 3) Return $re[0] EndFunc ;==>_getName wheres the mistake? Link to comment Share on other sites More sharing options...
PsaltyDS Posted July 2, 2008 Share Posted July 2, 2008 In my hurry, i never saw xenobiologist's reply, heres wht I have, i get a "subscript used with non-array variable" exception heres wht i have : ============ $remip = guictrlread($ip1) $passfile = Fileopen("somtext.txt", 0) $string = fileread($passfile) MsgBox(0,0, StringStripCR(_getName($string, $remip))) Func _getName($string, $suche) Local $re = StringRegExp($string, $suche & '\s*\|\|\s*(.*)', 3) Return $re[0] EndFunc ;==>_getName wheres the mistake? If you didn't get an array back from StringRegExp() you can check @error and @extended to find out why (see the help file). Since the example posted by xenobiologist works fine, the problem is with the data you are passing to it. Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law Link to comment Share on other sites More sharing options...
kris06 Posted July 3, 2008 Author Share Posted July 3, 2008 If you didn't get an array back from StringRegExp() you can check @error and @extended to find out why (see the help file). Since the example posted by xenobiologist works fine, the problem is with the data you are passing to it. muttley I did solve the problem , heres wht i did. $newip = "192.168.1.1" For $x = 1 to $array1[0] If StringInStr($array1[$X], $newip) Then $passline=FileReadLine($filepass, $x) $passrem = StringSplit($passline,",") $asspas = $passrem[2] fileclose($filepass) $filepass ="sometext.txt" $newline = StringReplace($passline,$asspas,$newp) $su = _ReplaceStringInFile($filepass,$passline,$newline) fileclose($filepass) EndIf Next The subscript error because I had opened the file in write mode, was trying to read and write at the same time, hence the anomaly. Thanks a lot everyone. !! 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