Sign in to follow this  
Followers 0
kris06

Find a partial string in a file and retrieve the entire line

11 posts in this topic

Hi all,

I have a file which contains data in the following order:

file.txt:

=====

192.168.1.1 || root

192.168.1.2 || admin

....etc

my 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 then

retrieve 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

Share this post


Link to post
Share on other sites



Hi all,

I have a file which contains data in the following order:

file.txt:

=====

192.168.1.1 || root

192.168.1.2 || admin

....etc

my 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 then

retrieve 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

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

Share this post


Link to post
Share on other sites

Pseudocode:

_FileReadToArray

For $X = 1 to $Array[0]

If StringInStr Then

EndIf

Next

Share this post


Link to post
Share on other sites

#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

Share this post


Link to post
Share on other sites

#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?

Share this post


Link to post
Share on other sites

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")

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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. :)!!

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